Compare commits
No commits in common. "51a7f8cb493f99a1f6d248e7a52f8a3515a46f06" and "61af64c72e8fa637590d15bc27e761cba29a9b7b" have entirely different histories.
51a7f8cb49
...
61af64c72e
@ -1,4 +1,4 @@
|
|||||||
# M4 - Aggregation and Indexing
|
# M4 - Aggregation
|
||||||
|
|
||||||
**Problem Statement:**
|
**Problem Statement:**
|
||||||
Design and Develop MongoDB Queries using Aggregation operations:
|
Design and Develop MongoDB Queries using Aggregation operations:
|
||||||
@ -90,7 +90,7 @@ db.Employee.insertMany([
|
|||||||
Expertise: ['Blockchain', 'Hashing', 'Encryption', 'Nerd'],
|
Expertise: ['Blockchain', 'Hashing', 'Encryption', 'Nerd'],
|
||||||
DOB: new Date("1949-12-28"),
|
DOB: new Date("1949-12-28"),
|
||||||
Email: "tanmay.m@wayne.com",
|
Email: "tanmay.m@wayne.com",
|
||||||
Contact: 9972410426,
|
Contact: 9972410425,
|
||||||
Address: [{PAddr: "Viman Nagar, Pune"}, {LAddr: "Viman Nagar, Pune", Pin_code: 411001}]
|
Address: [{PAddr: "Viman Nagar, Pune"}, {LAddr: "Viman Nagar, Pune", Pin_code: 411001}]
|
||||||
}
|
}
|
||||||
])
|
])
|
@ -1,217 +0,0 @@
|
|||||||
# M5 - Aggregation and Indexing
|
|
||||||
|
|
||||||
**Problem Statement:**
|
|
||||||
Design and Develop MongoDB Queries using Aggregation operations:
|
|
||||||
Create Employee collection by considering following Fields:
|
|
||||||
i. Emp_id : Number
|
|
||||||
ii. Name: Embedded Doc (FName, LName)
|
|
||||||
iii. Company Name: String
|
|
||||||
iv. Salary: Number
|
|
||||||
v. Designation: String
|
|
||||||
vi. Age: Number
|
|
||||||
vii. Expertise: Array
|
|
||||||
viii. DOB: String or Date
|
|
||||||
ix. Email id: String
|
|
||||||
x. Contact: String
|
|
||||||
xi. Address: Array of Embedded Doc (PAddr, LAddr)
|
|
||||||
Insert at least 5 documents in collection by considering above
|
|
||||||
attribute and execute following:
|
|
||||||
1. Using aggregation Return separates value in the Expertise array
|
|
||||||
and return sum of each element of array.
|
|
||||||
2. Using Aggregate method return Max and Min Salary for each
|
|
||||||
company.
|
|
||||||
3. Using Aggregate method find Employee with Total Salary for Each
|
|
||||||
City with Designation="DBA".
|
|
||||||
4. Using aggregation method Return separates value in the Expertise
|
|
||||||
array for employee name where Swapnil Jadhav
|
|
||||||
5. To Create Compound Indexes on Name: 1, Age: -1
|
|
||||||
6. Create an Index on Emp_id field, compare the time require to
|
|
||||||
search Emp_id before and after creating an index. (Hint Add at
|
|
||||||
least 10000 Documents)
|
|
||||||
7. Return a List of Indexes on created on employee Collection.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## Creating database & collection:
|
|
||||||
|
|
||||||
```json
|
|
||||||
use empDB3
|
|
||||||
db.createCollection("Employee")
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
## Inserting data:
|
|
||||||
|
|
||||||
```json
|
|
||||||
db.Employee.insertMany([
|
|
||||||
{
|
|
||||||
Name: {FName: "Ayush", LName: "Kalaskar"},
|
|
||||||
Company: "TCS",
|
|
||||||
Salary: 45000,
|
|
||||||
Designation: "Programmer",
|
|
||||||
Age: 24,
|
|
||||||
Expertise: ['Docker', 'Linux', 'Networking', 'Politics'],
|
|
||||||
DOB: new Date("1998-03-12"),
|
|
||||||
Email: "ayush.k@tcs.com",
|
|
||||||
Contact: 9972410427,
|
|
||||||
Address: [{PAddr: "Kokan, Maharashtra"}, {LAddr: "Lohegaon, Pune", Pin_code: 411014}]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: {FName: "Mehul", LName: "Patil"},
|
|
||||||
Company: "MEPA",
|
|
||||||
Salary: 55000,
|
|
||||||
Designation: "Tester",
|
|
||||||
Age: 20,
|
|
||||||
Expertise: ['HTML', 'CSS', 'Javascript', 'Teaching'],
|
|
||||||
DOB: new Date("1964-06-22"),
|
|
||||||
Email: "mehul.p@mepa.com",
|
|
||||||
Contact: 9972410426,
|
|
||||||
Address: [{PAddr: "NDB, Maharashtra"}, {LAddr: "Camp, Pune", Pin_code: 411001}]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: {FName: "Himanshu", LName: "Patil"},
|
|
||||||
Company: "Infosys",
|
|
||||||
Salary: 85000,
|
|
||||||
Designation: "Developer",
|
|
||||||
Age: 67,
|
|
||||||
Expertise: ['Mongodb', 'Mysql', 'Cassandra', 'Farming'],
|
|
||||||
DOB: new Date("1957-04-28"),
|
|
||||||
Email: "himanshu.p@infosys.com",
|
|
||||||
Contact: 9972410425,
|
|
||||||
Address: [{PAddr: "NDB, Maharashtra"}, {LAddr: "Camp, Pune", Pin_code: 411001}]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: {FName: "Swapnil", LName: "Jadhav"},
|
|
||||||
Company: "Wayne Industries",
|
|
||||||
Salary: 95000,
|
|
||||||
Designation: "DBA",
|
|
||||||
Age: 75,
|
|
||||||
Expertise: ['Blockchain', 'Hashing', 'Encryption', 'Nerd'],
|
|
||||||
DOB: new Date("1949-12-28"),
|
|
||||||
Email: "swapnil.j@wayne.com",
|
|
||||||
Contact: 9972410427,
|
|
||||||
Address: [{PAddr: "Viman Nagar, Pune"}, {LAddr: "Viman Nagar, Pune", Pin_code: 411001}]
|
|
||||||
}
|
|
||||||
])
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
## Queries
|
|
||||||
|
|
||||||
1. Using aggregation Return separates value in the Expertise array and return sum of each element of array.
|
|
||||||
```json
|
|
||||||
db.Employee.aggregate([
|
|
||||||
{
|
|
||||||
$unwind: "$Expertise"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$group: {
|
|
||||||
_id: "$Expertise",
|
|
||||||
count: { $sum: 1 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
])
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
2. Using Aggregate method return Max and Min Salary for each company.
|
|
||||||
```json
|
|
||||||
db.Employee.aggregate([
|
|
||||||
{
|
|
||||||
$group: {
|
|
||||||
_id: "$Company",
|
|
||||||
MIN: { $min: "$Salary" },
|
|
||||||
MAX: { $max: "$Salary" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
])
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
3. Using Aggregate method find Employee with Total Salary for Each City with Designation="DBA".
|
|
||||||
```json
|
|
||||||
db.Employee.aggregate([
|
|
||||||
{
|
|
||||||
$match: {
|
|
||||||
Designation: "DBA"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$group: {
|
|
||||||
_id: "$Address.PAddr",
|
|
||||||
Total: { $sum: "$Salary" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
])
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
4. Using aggregation method Return separates value in the Expertise array for employee name where Swapnil Jadhav
|
|
||||||
```json
|
|
||||||
db.Employee.aggregate([
|
|
||||||
{
|
|
||||||
$match: {
|
|
||||||
"Name.FName": "Swapnil",
|
|
||||||
"Name.LName": "Jadhav"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$unwind: "$Expertise"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
$group: {
|
|
||||||
_id: "$Expertise"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
])
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
5. To Create Compound Indexes on Name: 1, Age: -1
|
|
||||||
```json
|
|
||||||
db.Employee.createIndex({Name: 1, Age: -1})
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
6. Create an Index on Emp_id field, compare the time require to search Emp_id before and after creating an index. (Hint Add at least 10000 Documents)
|
|
||||||
```json
|
|
||||||
// Creating 10000 documents
|
|
||||||
for (let i=1; i<=10000; i++) {
|
|
||||||
db.Employee.insertOne({
|
|
||||||
Emp_id: i,
|
|
||||||
Name: `Employee ${i}`,
|
|
||||||
Designation: `Work ${i*5}`
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Wait for it to insert 10000 documents!
|
|
||||||
|
|
||||||
// Time without index
|
|
||||||
let startTime = new Date();
|
|
||||||
db.Employee.find( { Emp_id: 7500 } );
|
|
||||||
let endTime = new Date();
|
|
||||||
print("Time taken to search before index: " + (endTime - startTime) + "ms");
|
|
||||||
|
|
||||||
// Creating index on Emp_id
|
|
||||||
db.Employee.createIndex( { Emp_id: 1 } )
|
|
||||||
|
|
||||||
// Time with index
|
|
||||||
startTime = new Date();
|
|
||||||
db.Employee.find( { Emp_id: 7500 } );
|
|
||||||
endTime = new Date();
|
|
||||||
print("Time taken to search after index: " + (endTime - startTime) + "ms")
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
<details>
|
|
||||||
<summary>Output for query 6:</summary>
|
|
||||||
Time taken to search before index: 57ms<br>
|
|
||||||
Time taken to search after index: 35ms
|
|
||||||
</details>
|
|
||||||
|
|
||||||
7. Return a List of Indexes on created on employee Collection.
|
|
||||||
```json
|
|
||||||
db.Employee.getIndexes();
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
Loading…
Reference in New Issue
Block a user