Added content.
- Notes - Practical (Databases, Handouts, Queries, Softcopies, Write-ups) - Question Papers - DISCLAIMER file and motto Lastly, updated README file.
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
# M1 - Crud Operations
|
||||
|
||||
**Problem Statement:**
|
||||
Design and Develop MongoDB Queries using CRUD operations:
|
||||
Create Employee collection by considering following Fields:
|
||||
i. Name: Embedded Doc (FName, LName)
|
||||
ii. Company Name: String
|
||||
iii. Salary: Number
|
||||
iv. Designation: String
|
||||
v. Age: Number
|
||||
vi. Expertise: Array
|
||||
vii. DOB: String or Date
|
||||
viii. Email id: String
|
||||
ix. Contact: String
|
||||
x. Address: Array of Embedded Doc (PAddr, LAddr)
|
||||
|
||||
Insert at least 5 documents in collection by considering above attribute and execute following queries:
|
||||
1. Select all documents where the Designation field has the value
|
||||
"Programmer" and the value of the salary field is greater than
|
||||
30000.
|
||||
2. Creates a new document if no document in the employee collection
|
||||
contains
|
||||
{Designation: "Tester", Company_name: "TCS", Age: 25}
|
||||
3. Increase salary of each Employee working with “Infosys" 10000.
|
||||
4. Finds all employees working with "TCS" and reduce their salary
|
||||
by 5000.
|
||||
5. Return documents where Designation is not equal to "Tester".
|
||||
6. Find all employee with Exact Match on an Array having Expertise:
|
||||
['Mongodb','Mysql','Cassandra']
|
||||
|
||||
---
|
||||
|
||||
## Creating database & collection:
|
||||
|
||||
```json
|
||||
use empDB1
|
||||
db.createCollection("Employee")
|
||||
|
||||
```
|
||||
|
||||
## Inserting data:
|
||||
|
||||
```json
|
||||
db.Employee.insertMany([
|
||||
{
|
||||
Name: {FName: "Ayush", LName: "Kalaskar"},
|
||||
Company: "TCS",
|
||||
Salary: 45000,
|
||||
Designation: "Programmer",
|
||||
Age: 55,
|
||||
Expertise: ['Docker', 'Linux', 'Networking', 'Politics'],
|
||||
DOB: new Date("1969-03-12"),
|
||||
Email: "ayush.k@tcs.com",
|
||||
Contact: 9972410427,
|
||||
Address: [{PAddr: "Kokan, Maharashtra"}, {LAddr: "Lohegaon, Pune"}]
|
||||
},
|
||||
{
|
||||
Name: {FName: "Mehul", LName: "Patil"},
|
||||
Company: "MEPA",
|
||||
Salary: 55000,
|
||||
Designation: "Tester",
|
||||
Age: 60,
|
||||
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"}]
|
||||
},
|
||||
{
|
||||
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"}]
|
||||
}
|
||||
])
|
||||
|
||||
```
|
||||
|
||||
## Queries
|
||||
|
||||
1. Select all documents where the Designation field has the value "Programmer" and the value of the salary field is greater than 30000.
|
||||
```json
|
||||
db.Employee.find(
|
||||
{ Designation: "Programmer", Salary: { $gt: 30000 } }
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
2. Creates a new document if no document in the employee collection contains `{Designation: "Tester", Company_name: "TCS", Age: 25}`
|
||||
```json
|
||||
db.Employee.updateOne(
|
||||
{ Designation: "Tester", Company: "TCS", Age: 25 },
|
||||
{ $setOnInsert: {
|
||||
|
||||
Name: {FName: "Karan", LName: "Salvi"},
|
||||
Salary: 67500,
|
||||
Expertise: ['Blockchain', 'C++', 'Python', 'Fishing'],
|
||||
DOB: new Date("1999-11-01"),
|
||||
Email: "karan.s@tcs.com",
|
||||
Contact: 9972410424,
|
||||
Address: [{PAddr: "Kolhapur, Maharashtra"}, {LAddr: "Viman Nagar, Pune"}]
|
||||
}
|
||||
},
|
||||
{ upsert: true }
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
3. Increase salary of each Employee working with “Infosys" 10000.
|
||||
```json
|
||||
db.Employee.updateMany(
|
||||
{ Company: "Infosys" },
|
||||
{ $inc: { Salary: 10000 } }
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
4. Finds all employees working with "TCS" and reduce their salary by 5000.
|
||||
```json
|
||||
db.Employee.updateMany(
|
||||
{ Company: "TCS" },
|
||||
{ $inc: { Salary: -5000 } }
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
5. Return documents where Designation is not equal to "Tester".
|
||||
```json
|
||||
db.Employee.find(
|
||||
{ Designation: { $ne: "Tester"} }
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
6. Find all employee with Exact Match on an Array having Expertise: `['Mongodb','Mysql','Cassandra']`
|
||||
```json
|
||||
db.Employee.find(
|
||||
{ Expertise: { $all: ['Mongodb', 'Mysql', 'Cassandra'] } }
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
@@ -0,0 +1,160 @@
|
||||
# M2 - Crud Operations
|
||||
|
||||
**Problem Statement:**
|
||||
Design and Develop MongoDB Queries using CRUD operations:
|
||||
Create Employee collection by considering following Fields:
|
||||
i. Name: Embedded Doc (FName, LName)
|
||||
ii. Company Name: String
|
||||
iii. Salary: Number
|
||||
iv. Designation: String
|
||||
v. Age: Number
|
||||
vi. Expertise: Array
|
||||
vii. DOB: String or Date
|
||||
viii. Email id: String
|
||||
ix. Contact: String
|
||||
x. Address: Array of Embedded Doc (PAddr, LAddr)
|
||||
Insert at least 5 documents in collection by considering above
|
||||
attribute and execute following queries:
|
||||
1. Final name of Employee where age is less than 30 and salary more
|
||||
than 50000.
|
||||
2. Creates a new document if no document in the employee collection
|
||||
contains
|
||||
{Designation: "Tester", Company_name: "TCS", Age: 25}
|
||||
3. Selects all documents in the collection where the field age has
|
||||
a value less than 30 or the value of the salary field is greater
|
||||
than 40000.
|
||||
4. Find documents where Designation is not equal to "Developer".
|
||||
5. Find _id, Designation, Address and Name from all documents where
|
||||
Company_name is "Infosys".
|
||||
6. Display only FName and LName of all Employees
|
||||
|
||||
---
|
||||
|
||||
## Creating database & collection:
|
||||
|
||||
```json
|
||||
use empDB1
|
||||
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"}]
|
||||
},
|
||||
{
|
||||
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"}]
|
||||
},
|
||||
{
|
||||
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"}]
|
||||
}
|
||||
])
|
||||
|
||||
```
|
||||
|
||||
## Queries
|
||||
|
||||
1. Final name of Employee where age is less than 30 and salary more than 50000.
|
||||
```json
|
||||
db.Employee.find(
|
||||
{
|
||||
Age: { $lt: 30 },
|
||||
Salary: { $gt: 50000 }
|
||||
}
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
2. Creates a new document if no document in the employee collection contains `{Designation: "Tester", Company_name: "TCS", Age: 25}`
|
||||
```json
|
||||
db.Employee.updateOne(
|
||||
{Designation: "Tester", Company: "TCS", Age: 25},
|
||||
{ $setOnInsert:
|
||||
{
|
||||
Name: {FName: "Karan", LName: "Salvi"},
|
||||
Salary: 35000,
|
||||
Expertise: ['Blockchain', 'C++', 'Python', 'Fishing'],
|
||||
DOB: new Date("1999-11-01"),
|
||||
Email: "karan.s@tcs.com",
|
||||
Contact: 9972410424,
|
||||
Address: [{PAddr: "Kolhapur, Maharashtra"}, {LAddr: "Viman Nagar, Pune"}]
|
||||
}
|
||||
},
|
||||
{ upsert: true }
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
3. Selects all documents in the collection where the field age has a value less than 30 or the value of the salary field is greater than 40000.
|
||||
```json
|
||||
db.Employee.find(
|
||||
{ $or:
|
||||
[
|
||||
{ Age: { $lt: 30 } },
|
||||
{ Salary: { $gt: 40000 } }
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
4. Find documents where Designation is not equal to "Developer".
|
||||
```json
|
||||
db.Employee.find(
|
||||
{
|
||||
Designation: { $ne: "Developer" }
|
||||
}
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
5. Find _id, Designation, Address and Name from all documents where Company_name is "Infosys".
|
||||
```json
|
||||
db.Employee.find(
|
||||
{ Company: "Infosys" },
|
||||
{ _id: 1, Designation: 1, Address: 1, Name: 1 }
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
6. Display only FName and LName of all Employees.
|
||||
```json
|
||||
db.Employee.find(
|
||||
{},
|
||||
{"Name.FName": 1, "Name.LName": 1}
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
# M3 - Crud Operations
|
||||
|
||||
**Problem Statement:**
|
||||
Design and Develop MongoDB Queries using CRUD 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 queries:
|
||||
1. Creates a new document if no document in the employee collection
|
||||
contains
|
||||
{Designation: "Tester", Company_name: "TCS", Age: 25}
|
||||
2. Finds all employees working with Company_name: "TCS" and
|
||||
increase their salary by 2000.
|
||||
3. Matches all documents where the value of the field Address is an
|
||||
embedded document that contains only the field city with the
|
||||
value "Pune" and the field Pin_code with the value "411001".
|
||||
4. Find employee details who are working as "Developer" or
|
||||
"Tester".
|
||||
5. Drop Single documents where designation="Developer".
|
||||
6. Count number of documents in 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}]
|
||||
}
|
||||
])
|
||||
|
||||
```
|
||||
|
||||
## Queries
|
||||
|
||||
1. Creates a new document if no document in the employee collection contains `{Designation: "Tester", Company_name: "TCS", Age: 25}`
|
||||
```json
|
||||
db.Employee.updateOne(
|
||||
{Designation: "Tester", Company: "TCS", Age: 25},
|
||||
{ $setOnInsert: {
|
||||
Name: {FName: "Karan", LName: "Salvi"},
|
||||
Salary: 67500,
|
||||
Expertise: ['Blockchain', 'C++', 'Python', 'Fishing'],
|
||||
DOB: new Date("1999-11-01"),
|
||||
Email: "karan.s@tcs.com",
|
||||
Contact: 9972410424,
|
||||
Address: [{PAddr: "Kolhapur, Maharashtra"}, {LAddr: "Viman Nagar, Pune", Pin_code: 411014}]
|
||||
}
|
||||
},
|
||||
{ upsert: true }
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
2. Finds all employees working with Company_name: "TCS" and increase their salary by 2000.
|
||||
```json
|
||||
db.Employee.updateMany(
|
||||
{ Company: "TCS" },
|
||||
{ $inc: { Salary: 2000 } }
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
3. Matches all documents where the value of the field Address is an embedded document that contains only the field city with the value "Pune" and the field Pin_code with the value "411001".
|
||||
```json
|
||||
db.Employee.find(
|
||||
{ $or: [
|
||||
{
|
||||
"Address.Pin_code": 411001,
|
||||
"Address.LAddr": { $regex: /Pune/i }
|
||||
},
|
||||
{
|
||||
"Address.Pin_code": 411001,
|
||||
"Address.PAddr": { $regex: /Pune/i }
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
4. Find employee details who are working as "Developer" or "Tester".
|
||||
```json
|
||||
db.Employee.find(
|
||||
{ $or: [
|
||||
{ Designation: "Developer" },
|
||||
{ Designation: "Tester" }
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
```
|
||||
|
||||
5. Drop Single documents where Designation="Developer"
|
||||
```json
|
||||
db.Employee.deleteOne( { Designation: "Developer" } )
|
||||
|
||||
```
|
||||
|
||||
6. Count number of documents in employee collection.
|
||||
```json
|
||||
db.Employee.countDocuments();
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
@@ -0,0 +1,205 @@
|
||||
# M4 - 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 Designation with Total Salary is Above
|
||||
200000.
|
||||
2. Using Aggregate method returns names and _id in upper case and
|
||||
in alphabetical order.
|
||||
3. Using aggregation method find Employee with Total Salary for
|
||||
Each City with Designation="DBA".
|
||||
4. Create Single Field Indexes on Designation field of employee
|
||||
collection
|
||||
5. To Create Multikey Indexes on Expertise field of employee
|
||||
collection.
|
||||
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 empDB2
|
||||
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: "Tanmay", LName: "Macho"},
|
||||
Company: "Wayne Industries",
|
||||
Salary: 95000,
|
||||
Designation: "DBA",
|
||||
Age: 75,
|
||||
Expertise: ['Blockchain', 'Hashing', 'Encryption', 'Nerd'],
|
||||
DOB: new Date("1949-12-28"),
|
||||
Email: "tanmay.m@wayne.com",
|
||||
Contact: 9972410426,
|
||||
Address: [{PAddr: "Viman Nagar, Pune"}, {LAddr: "Viman Nagar, Pune", Pin_code: 411001}]
|
||||
}
|
||||
])
|
||||
|
||||
```
|
||||
|
||||
## Queries
|
||||
|
||||
1. Using aggregation Return Designation with Total Salary is Above 200000.
|
||||
```json
|
||||
db.Employee.aggregate([
|
||||
{
|
||||
$group: {
|
||||
_id: "$Designation",
|
||||
TotalSalary: { $sum: "$Salary" }
|
||||
}
|
||||
},
|
||||
{
|
||||
$match: {
|
||||
TotalSalary: { $gt: 20000 }
|
||||
}
|
||||
}
|
||||
])
|
||||
|
||||
```
|
||||
|
||||
2. Using Aggregate method returns names and _id in upper case and in alphabetical order.
|
||||
```json
|
||||
db.Employee.aggregate([
|
||||
{
|
||||
$project: {
|
||||
_id: 1,
|
||||
Name: { $toUpper: { $concat: [ "$Name.FName", " ", "$Name.LName" ] } }
|
||||
}
|
||||
},
|
||||
{ $sort: { Name: 1 } }
|
||||
])
|
||||
|
||||
```
|
||||
|
||||
3. Using aggregation method find Employee with Total Salary for Each City with Designation="DBA".
|
||||
```json
|
||||
db.Employee.aggregate([
|
||||
{
|
||||
$match: {
|
||||
Designation: "DBA"
|
||||
}
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: "$Address.PAddr",
|
||||
Salary: { $sum: "$Salary" }
|
||||
}
|
||||
}
|
||||
])
|
||||
|
||||
```
|
||||
|
||||
4. Create Single Field Indexes on Designation field of employee collection
|
||||
```json
|
||||
db.Employee.createIndex( { Designation: 1 } )
|
||||
|
||||
```
|
||||
|
||||
5. To Create Multikey Indexes on Expertise field of employee collection.
|
||||
```json
|
||||
db.Employee.createIndex( { Expertise: 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
|
||||
// Adding 1000 employees
|
||||
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 without 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 with index: " + (endTime - startTime) + " ms");
|
||||
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Output for query 6:</summary>
|
||||
Time taken to search without index: 41 ms<br>
|
||||
Time taken to search with index: 29 ms<br>
|
||||
</details>
|
||||
|
||||
7. Return a List of Indexes on created on employee Collection.
|
||||
```sql
|
||||
db.Employee.getIndexes()
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
@@ -0,0 +1,217 @@
|
||||
# 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();
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
@@ -0,0 +1,178 @@
|
||||
# M6 - Map-reduce
|
||||
|
||||
**Problem statement:**
|
||||
Design MongoDB database and perform following Map reduce operation:
|
||||
Create Employee collection by considering following Fields:
|
||||
i. Name: Embedded Doc (FName, LName)
|
||||
ii. Company Name: String
|
||||
iii. Salary: Number
|
||||
iv. Designation: String
|
||||
v. Age: Number
|
||||
vi. Expertise: Array
|
||||
vii. DOB: String or Date
|
||||
viii. Email id: String
|
||||
ix. Contact: String
|
||||
x. Address: Array of Embedded Doc (PAddr, LAddr)
|
||||
Execute the following query:
|
||||
|
||||
---
|
||||
|
||||
## Creating database
|
||||
```mongo
|
||||
use mapred;
|
||||
|
||||
```
|
||||
|
||||
## Inserting data:
|
||||
```mongo
|
||||
db.emp.insertMany([
|
||||
{
|
||||
Name: {FName: "Tanmay", LName: "Machkar"},
|
||||
Company: "TCS",
|
||||
Salary: 40000,
|
||||
Designation: "Tester",
|
||||
Age: 25,
|
||||
Expertise: ['Mongodb','Mysql','Cassandra'],
|
||||
DOB: new Date("2003-12-02"),
|
||||
Email: "xyz@gmail.com",
|
||||
Contact: 1234567890,
|
||||
Address: [{PAddr: {City: "Pune", Rd: "Bharatmata"}}, {LAddr: {Pin_code: 411001}}]
|
||||
},
|
||||
{
|
||||
Name: {FName: "Rajesh", LName: "Machkar"},
|
||||
Company: "Infosys",
|
||||
Salary: 50000,
|
||||
Designation: "Programmer",
|
||||
Age: 29,
|
||||
Expertise: ['Mongodb','Mysql'],
|
||||
DOB: new Date("1990-12-02"),
|
||||
Email: "xy@gmail.com",
|
||||
Contact: 1234567809,
|
||||
Address: [{PAddr: {City: "Pune", Rd: "JM"}}, {LAddr: {Pin_code: 411047}}]
|
||||
},
|
||||
{
|
||||
Name: {FName: "Tejas", LName: "Machkar"},
|
||||
Company: "TCS",
|
||||
Salary: 20000,
|
||||
Designation: "Developer",
|
||||
Age: 35,
|
||||
Expertise: ['Mongodb'],
|
||||
DOB: new Date("2000-11-22"),
|
||||
Email: "x@gmail.com",
|
||||
Contact: 1234567089,
|
||||
Address: [{PAddr: {City: "Mumbai", Rd: "Airport"}}, {LAddr: {Pin_code: 411030}}]
|
||||
},
|
||||
{
|
||||
Name: {FName: "Deepali", LName: "Machkar"},
|
||||
Company: "Persistent",
|
||||
Salary: 40000,
|
||||
Designation: "Tester",
|
||||
Age: 30,
|
||||
Expertise: ['Mysql'],
|
||||
DOB: new Date("1978-02-12"),
|
||||
Email: "y@gmail.com",
|
||||
Contact: 1234506789,
|
||||
Address: [{PAddr: {City: "Phaltan", Rd: "Highway"}}, {LAddr: {Pin_code: 411012}}]
|
||||
},
|
||||
{
|
||||
Name: {FName: "Om", LName: "Deokar"},
|
||||
Company: "Persistent",
|
||||
Salary: 25000,
|
||||
Designation: "Programmer",
|
||||
Age: 39,
|
||||
Expertise: ['Cassandra'],
|
||||
DOB: new Date("2007-03-15"),
|
||||
Email: "z@gmail.com",
|
||||
Contact: 1234567890,
|
||||
Address: [{PAddr: {City: "Jaipur", Rd: "Pink"}}, {LAddr: {Pin_code: 411056}}]
|
||||
},
|
||||
])
|
||||
```
|
||||
|
||||
## Queries
|
||||
|
||||
1. Display the total salary of per company.
|
||||
```mongo
|
||||
db.emp.mapReduce(
|
||||
function(){
|
||||
emit(this.Company, this.Salary);
|
||||
},
|
||||
function(key, values){
|
||||
return Array.sum(values);
|
||||
},
|
||||
{ out: "query1"}
|
||||
)
|
||||
db.query1.find()
|
||||
|
||||
```
|
||||
|
||||
2. Display the total salary of company Name:"TCS".
|
||||
```mongo
|
||||
db.emp.mapReduce(
|
||||
function(){
|
||||
if(this.Company == "TCS"){
|
||||
emit(this.Company, this.Salary);
|
||||
}
|
||||
},
|
||||
function(key, values){
|
||||
return Array.sum(values);
|
||||
},
|
||||
{ out: "query2"}
|
||||
)
|
||||
db.query2.find()
|
||||
|
||||
```
|
||||
|
||||
3. Return the average salary of company whose address is “Pune".
|
||||
```mongo
|
||||
db.emp.mapReduce(
|
||||
function(){
|
||||
if(this.Address[0].PAddr.City === "Pune"){
|
||||
emit(this.Company, this.Salary);
|
||||
}
|
||||
},
|
||||
function(key, values){
|
||||
return Array.avg(values);
|
||||
},
|
||||
{ out: "query3"}
|
||||
)
|
||||
db.query3.find()
|
||||
|
||||
```
|
||||
|
||||
4. Display total count for “City=Pune”.
|
||||
```mongo
|
||||
db.emp.mapReduce(
|
||||
function(){
|
||||
if(this.Address[0].PAddr.City === "Pune"){
|
||||
emit("Count", 1);
|
||||
}
|
||||
},
|
||||
function(key, values){
|
||||
return Array.sum(values);
|
||||
},
|
||||
{ out: "query4"}
|
||||
)
|
||||
db.query4.find()
|
||||
|
||||
```
|
||||
|
||||
5. Return count for city pune and age greater than 40.
|
||||
```mongo
|
||||
db.emp.mapReduce(
|
||||
function(){
|
||||
if(this.Address[0].PAddr.City === "Pune" && this.Age > 25){
|
||||
emit("Count", 1);
|
||||
}
|
||||
},
|
||||
function(key, values){
|
||||
return Array.sum(values);
|
||||
},
|
||||
{ out: "query5"}
|
||||
)
|
||||
db.query5.find()
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user