Added queries for B1 and B2.

This commit is contained in:
K 2024-10-16 11:27:45 +05:30
parent a5722f05cf
commit 126d0dbe8a
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
2 changed files with 384 additions and 0 deletions

View File

@ -0,0 +1,207 @@
# Queries-B1
## Creation
```mongodb
use empDB
db.createCollection("Employee")
```
## Inserting data
```mongodb
db.Employee.insertMany([
{
Empid: 1,
Name: { FName: "Rahul", LName: "Sharma" },
Company_name: "TCS",
Salary: 50000,
Designation: "Programmer",
Age: 28,
Expertise: ["Java", "Spring", "MongoDB"],
DOB: "1995-04-15",
Email_id: "rahul.sharma@example.com",
Contact: "9876543210",
Address: [{ PAddr: "123, Street A, Pune", LAddr: "Maharashtra" }]
},
{
Empid: 2,
Name: { FName: "Sita", LName: "Patel" },
Company_name: "Infosys",
Salary: 60000,
Designation: "Developer",
Age: 30,
Expertise: ["JavaScript", "React", "Node.js"],
DOB: "1993-06-25",
Email_id: "sita.patel@example.com",
Contact: "9876543211",
Address: [{ PAddr: "234, Street B, Bangalore", LAddr: "Karnataka" }]
},
{
Empid: 3,
Name: { FName: "Anil", LName: "Kumar" },
Company_name: "Wipro",
Salary: 45000,
Designation: "Tester",
Age: 29,
Expertise: ["Selenium", "Python"],
DOB: "1994-08-12",
Email_id: "anil.kumar@example.com",
Contact: "9876543212",
Address: [{ PAddr: "345, Street C, Hyderabad", LAddr: "Telangana" }]
},
{
Empid: 4,
Name: { FName: "Priya", LName: "Verma" },
Company_name: "Infosys",
Salary: 70000,
Designation: "Project Manager",
Age: 35,
Expertise: ["Agile", "Scrum"],
DOB: "1988-02-20",
Email_id: "priya.verma@example.com",
Contact: "9876543213",
Address: [{ PAddr: "456, Street D, Chennai", LAddr: "Tamil Nadu" }]
},
{
Empid: 5,
Name: { FName: "Raj", LName: "Singh" },
Company_name: "TCS",
Salary: 32000,
Designation: "Programmer",
Age: 27,
Expertise: ["Java", "Angular"],
DOB: "1996-03-30",
Email_id: "raj.singh@example.com",
Contact: "9876543214",
Address: [{ PAddr: "567, Street E, Delhi", LAddr: "Delhi" }]
},
{
Empid: 6,
Name: { FName: "Neha", LName: "Iyer" },
Company_name: "HCL",
Salary: 50000,
Designation: "Designer",
Age: 32,
Expertise: ["Photoshop", "Illustrator"],
DOB: "1991-11-11",
Email_id: "neha.iyer@example.com",
Contact: "9876543215",
Address: [{ PAddr: "678, Street F, Kolkata", LAddr: "West Bengal" }]
},
{
Empid: 7,
Name: { FName: "Karan", LName: "Bansal" },
Company_name: "TCS",
Salary: 45000,
Designation: "Tester",
Age: 26,
Expertise: ["Selenium", "Java"],
DOB: "1997-07-07",
Email_id: "karan.bansal@example.com",
Contact: "9876543216",
Address: [{ PAddr: "789, Street G, Pune", LAddr: "Maharashtra" }]
},
{
Empid: 8,
Name: { FName: "Aarti", LName: "Mehta" },
Company_name: "Accenture",
Salary: 80000,
Designation: "Architect",
Age: 33,
Expertise: ["Cloud", "Microservices"],
DOB: "1990-01-01",
Email_id: "aarti.mehta@example.com",
Contact: "9876543217",
Address: [{ PAddr: "890, Street H, Noida", LAddr: "Uttar Pradesh" }]
},
{
Empid: 9,
Name: { FName: "Vikram", LName: "Yadav" },
Company_name: "Infosys",
Salary: 40000,
Designation: "Developer",
Age: 31,
Expertise: ["C#", ".NET"],
DOB: "1992-05-05",
Email_id: "vikram.yadav@example.com",
Contact: "9876543218",
Address: [{ PAddr: "901, Street I, Jaipur", LAddr: "Rajasthan" }]
},
{
Empid: 10,
Name: { FName: "Sneha", LName: "Dutta" },
Company_name: "Wipro",
Salary: 39000,
Designation: "HR",
Age: 29,
Expertise: ["Recruitment", "Employee Relations"],
DOB: "1994-09-09",
Email_id: "sneha.dutta@example.com",
Contact: "9876543219",
Address: [{ PAddr: "1234, Street J, Ahmedabad", LAddr: "Gujarat" }]
}
])
```
## Queries
1. Select all documents where Designation is "Programmer" and Salary > 30000:
```mongodb
db.Employee.find({ Designation: "Programmer", Salary: { $gt: 30000 } })
```
2. Create a new document if no document contains `{Designation: "Tester", Company_name: "TCS", Age: 25}`:
```mongodb
db.Employee.update(
{ Designation: "Tester", Company_name: "TCS", Age: 25 },
{ $setOnInsert: { Empid: 11, Name: { FName: "New", LName: "Tester" }, Salary: 30000, DOB: "1998-01-01", Email_id: "new.tester@example.com", Contact: "9876543220", Address: [{ PAddr: "New Address", LAddr: "New City" }] } },
{ upsert: true }
)
```
3. Select all documents where Age < 30 or Salary > 40000:
```mongodb
db.Employee.find({ $or: [{ Age: { $lt: 30 } }, { Salary: { $gt: 40000 } }] })
```
4. Match documents where Address contains city "Pune" and Pin_code "411001":
```mongodb
db.Employee.find({ Address: { $elemMatch: { city: "Pune", Pin_code: "411001" } } })
```
5. Find all documents with Company_name "TCS" and modify their salary by 2000:
```mongodb
db.Employee.updateMany(
{ Company_name: "TCS" },
{ $inc: { Salary: 2000 } }
)
```
6. Find documents where Designation is not equal to "Developer":
```mongodb
db.Employee.find({ Designation: { $ne: "Developer" } })
```
7. Find _id, Designation, Address, and Name where Company_name is "Infosys":
```mongodb
db.Employee.find(
{ Company_name: "Infosys" },
{ _id: 1, Designation: 1, Address: 1, Name: 1 }
)
```
8. Select all documents where Designation is either "Developer" or "Tester":
```mongodb
db.Employee.find({ Designation: { $in: ["Developer", "Tester"] } })
```
9. Find all documents with exact match on Expertise array:
```mongodb
db.Employee.find({ Expertise: { $all: ['Mongodb', 'Mysql', 'Cassandra'] } })
```
10. Drop single documents where Designation is "Developer":
```mongodb
db.Employee.deleteMany({ Designation: "Developer" })
```

View File

@ -0,0 +1,177 @@
## Queries
### A
1. Return Designation with Total Salary Above 200000:
```mongodb
db.Employee.aggregate([
{
$group: {
_id: "$Designation",
TotalSalary: { $sum: "$Salary" }
}
},
{
$match: {
TotalSalary: { $gt: 200000 }
}
}
])
```
2. Find Employee with Total Salary for Each City with Designation "DBA":
```mongodb
db.Employee.aggregate([
{
$match: { Designation: "DBA" }
},
{
$group: {
_id: { $arrayElemAt: ["$Address.PAddr", 0] },
TotalSalary: { $sum: "$Salary" }
}
}
])
```
3. Find Total Salary of Employee with Designation "DBA" for Each Company:
```mongodb
db.Employee.aggregate([
{
$match: { Designation: "DBA" }
},
{
$group: {
_id: "$Company_name",
TotalSalary: { $sum: "$Salary" }
}
}
])
```
4. Returns Names and _id in Upper Case and in Alphabetical Order:
```mongodb
db.Employee.aggregate([
{
$project: {
_id: 1,
Name: { $toUpper: { $concat: ["$Name.FName", " ", "$Name.LName"] } }
}
},
{
$sort: { Name: 1 }
}
])
```
5. Count All Records from Collection:
```mongodb
db.Employee.countDocuments()
```
6. For Each Unique Designation, Find Avg Salary and Output Sorted by AvgSal:
```mongodb
db.Employee.aggregate([
{
$group: {
_id: "$Designation",
AvgSalary: { $avg: "$Salary" }
}
},
{
$sort: { AvgSalary: 1 }
}
])
```
7. Return Separate Value in the Expertise Array Where Name of Employee is "Swapnil":
```mongodb
db.Employee.aggregate([
{
$match: { "Name.FName": "Swapnil" }
},
{
$unwind: "$Expertise"
},
{
$project: { Expertise: 1 }
}
])
```
8. Return Separate Value in the Expertise Array and Return Sum of Each Element of Array:
```mongodb
db.Employee.aggregate([
{
$unwind: "$Expertise"
},
{
$group: {
_id: "$Expertise",
TotalCount: { $sum: 1 }
}
}
])
```
9. Return Array for Designation Whose Address is "Pune":
```mongodb
db.Employee.aggregate([
{
$match: { "Address.PAddr": { $regex: "Pune" } }
},
{
$project: { Designation: 1 }
}
])
```
10. Return Max and Min Salary for Each Company:
```mongodb
db.Employee.aggregate([
{
$group: {
_id: "$Company_name",
MaxSalary: { $max: "$Salary" },
MinSalary: { $min: "$Salary" }
}
}
])
```
### B
1. Create Single Field Indexes on Designation:
```mongodb
db.Employee.createIndex({ Designation: 1 })
```
2. Create Compound Indexes on Name and Age:
```mongodb
db.Employee.createIndex({ "Name.FName": 1, Age: -1 })
```
3. Create Multikey Indexes on Expertise Array:
```mongodb
db.Employee.createIndex({ Expertise: 1 })
```
4. Return a List of All Indexes on Collection:
```mongodb
db.Employee.getIndexes()
```
5. Rebuild Indexes:
```mongodb
db.Employee.reIndex()
```
6. Drop Index on Remove Specific Index:
```mongodb
db.Employee.dropIndex("empIndex")
```
7. Remove All Indexes Except for the _id Index from a Collection:
```mongodb
db.Employee.dropIndexes()
```