Made changes to names, company name in sample data and made minor visual fixes.

This commit is contained in:
K 2024-10-17 12:58:11 +05:30
parent 126d0dbe8a
commit 77fcb85901
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F

View File

@ -5,6 +5,7 @@
```mongodb ```mongodb
use empDB use empDB
db.createCollection("Employee") db.createCollection("Employee")
``` ```
## Inserting data ## Inserting data
@ -13,131 +14,131 @@ db.createCollection("Employee")
db.Employee.insertMany([ db.Employee.insertMany([
{ {
Empid: 1, Empid: 1,
Name: { FName: "Rahul", LName: "Sharma" }, Name: { FName: "Ayush", LName: "Kalaskar" },
Company_name: "TCS", Company_name: "Oscorp",
Salary: 50000, Salary: 50000,
Designation: "Programmer", Designation: "Programmer",
Age: 28, Age: 28,
Expertise: ["Java", "Spring", "MongoDB"], Expertise: ["Java", "Spring", "MongoDB"],
DOB: "1995-04-15", DOB: "1995-04-15",
Email_id: "rahul.sharma@example.com", Email_id: "ayush.kalaskar@oscorp.com",
Contact: "9876543210", Contact: "9876543210",
Address: [{ PAddr: "123, Street A, Pune", LAddr: "Maharashtra" }] Address: [{ PAddr: "123, Street A, Pune", LAddr: "Maharashtra" }]
}, },
{ {
Empid: 2, Empid: 2,
Name: { FName: "Sita", LName: "Patel" }, Name: { FName: "Himanshu", LName: "Patil" },
Company_name: "Infosys", Company_name: "Hammer Industries",
Salary: 60000, Salary: 60000,
Designation: "Developer", Designation: "Developer",
Age: 30, Age: 30,
Expertise: ["JavaScript", "React", "Node.js"], Expertise: ["JavaScript", "React", "Node.js"],
DOB: "1993-06-25", DOB: "1993-06-25",
Email_id: "sita.patel@example.com", Email_id: "himanshu.patil@hammerindustries.com",
Contact: "9876543211", Contact: "9876543211",
Address: [{ PAddr: "234, Street B, Bangalore", LAddr: "Karnataka" }] Address: [{ PAddr: "234, Street B, Bangalore", LAddr: "Karnataka" }]
}, },
{ {
Empid: 3, Empid: 3,
Name: { FName: "Anil", LName: "Kumar" }, Name: { FName: "Mehul", LName: "Patil" },
Company_name: "Wipro", Company_name: "Lex Corp.",
Salary: 45000, Salary: 45000,
Designation: "Tester", Designation: "Tester",
Age: 29, Age: 29,
Expertise: ["Selenium", "Python"], Expertise: ["Selenium", "Python"],
DOB: "1994-08-12", DOB: "1994-08-12",
Email_id: "anil.kumar@example.com", Email_id: "mehul.patil@lexcorp.org",
Contact: "9876543212", Contact: "9876543212",
Address: [{ PAddr: "345, Street C, Hyderabad", LAddr: "Telangana" }] Address: [{ PAddr: "345, Street C, Hyderabad", LAddr: "Telangana" }]
}, },
{ {
Empid: 4, Empid: 4,
Name: { FName: "Priya", LName: "Verma" }, Name: { FName: "Tanmay", LName: "Machkar" },
Company_name: "Infosys", Company_name: "Wayne Industries",
Salary: 70000, Salary: 70000,
Designation: "Project Manager", Designation: "Project Manager",
Age: 35, Age: 35,
Expertise: ["Agile", "Scrum"], Expertise: ["Agile", "Scrum"],
DOB: "1988-02-20", DOB: "1988-02-20",
Email_id: "priya.verma@example.com", Email_id: "tanmay.machkar@batmobile.com",
Contact: "9876543213", Contact: "9876543213",
Address: [{ PAddr: "456, Street D, Chennai", LAddr: "Tamil Nadu" }] Address: [{ PAddr: "456, Street D, Chennai", LAddr: "Tamil Nadu" }]
}, },
{ {
Empid: 5, Empid: 5,
Name: { FName: "Raj", LName: "Singh" }, Name: { FName: "Rajendra", LName: "Patil" },
Company_name: "TCS", Company_name: "Stark Industries",
Salary: 32000, Salary: 32000,
Designation: "Programmer", Designation: "Programmer",
Age: 27, Age: 27,
Expertise: ["Java", "Angular"], Expertise: ["Java", "Angular"],
DOB: "1996-03-30", DOB: "1996-03-30",
Email_id: "raj.singh@example.com", Email_id: "rajendra.patil@starkindustries.com",
Contact: "9876543214", Contact: "9876543214",
Address: [{ PAddr: "567, Street E, Delhi", LAddr: "Delhi" }] Address: [{ PAddr: "567, Street E, Delhi", LAddr: "Delhi" }]
}, },
{ {
Empid: 6, Empid: 6,
Name: { FName: "Neha", LName: "Iyer" }, Name: { FName: "Rajesh", LName: "Patil" },
Company_name: "HCL", Company_name: "Roxonn",
Salary: 50000, Salary: 50000,
Designation: "Designer", Designation: "Designer",
Age: 32, Age: 32,
Expertise: ["Photoshop", "Illustrator"], Expertise: ["Photoshop", "Illustrator"],
DOB: "1991-11-11", DOB: "1991-11-11",
Email_id: "neha.iyer@example.com", Email_id: "rajesh.patil@roxonn.com",
Contact: "9876543215", Contact: "9876543215",
Address: [{ PAddr: "678, Street F, Kolkata", LAddr: "West Bengal" }] Address: [{ PAddr: "678, Street F, Kolkata", LAddr: "West Bengal" }]
}, },
{ {
Empid: 7, Empid: 7,
Name: { FName: "Karan", LName: "Bansal" }, Name: { FName: "Prashant", LName: "Kalaskar" },
Company_name: "TCS", Company_name: "Y-Space",
Salary: 45000, Salary: 45000,
Designation: "Tester", Designation: "Tester",
Age: 26, Age: 26,
Expertise: ["Selenium", "Java"], Expertise: ["Selenium", "Java"],
DOB: "1997-07-07", DOB: "1997-07-07",
Email_id: "karan.bansal@example.com", Email_id: "prashant.kalaskar@y-space.com",
Contact: "9876543216", Contact: "9876543216",
Address: [{ PAddr: "789, Street G, Pune", LAddr: "Maharashtra" }] Address: [{ PAddr: "789, Street G, Pune", LAddr: "Maharashtra" }]
}, },
{ {
Empid: 8, Empid: 8,
Name: { FName: "Aarti", LName: "Mehta" }, Name: { FName: "Aditya", LName: "Gundeti" },
Company_name: "Accenture", Company_name: "SNASA",
Salary: 80000, Salary: 80000,
Designation: "Architect", Designation: "Architect",
Age: 33, Age: 33,
Expertise: ["Cloud", "Microservices"], Expertise: ["Cloud", "Microservices"],
DOB: "1990-01-01", DOB: "1990-01-01",
Email_id: "aarti.mehta@example.com", Email_id: "aditya.gundeti@snasa.org",
Contact: "9876543217", Contact: "9876543217",
Address: [{ PAddr: "890, Street H, Noida", LAddr: "Uttar Pradesh" }] Address: [{ PAddr: "890, Street H, Noida", LAddr: "Uttar Pradesh" }]
}, },
{ {
Empid: 9, Empid: 9,
Name: { FName: "Vikram", LName: "Yadav" }, Name: { FName: "Manoj", LName: "Patil" },
Company_name: "Infosys", Company_name: "MEPA",
Salary: 40000, Salary: 40000,
Designation: "Developer", Designation: "Developer",
Age: 31, Age: 31,
Expertise: ["C#", ".NET"], Expertise: ["C#", ".NET"],
DOB: "1992-05-05", DOB: "1992-05-05",
Email_id: "vikram.yadav@example.com", Email_id: "manoj.patil@mepa.com",
Contact: "9876543218", Contact: "9876543218",
Address: [{ PAddr: "901, Street I, Jaipur", LAddr: "Rajasthan" }] Address: [{ PAddr: "901, Street I, Jaipur", LAddr: "Rajasthan" }]
}, },
{ {
Empid: 10, Empid: 10,
Name: { FName: "Sneha", LName: "Dutta" }, Name: { FName: "Afan", LName: "Shaikh" },
Company_name: "Wipro", Company_name: "Vought",
Salary: 39000, Salary: 39000,
Designation: "HR", Designation: "HR",
Age: 29, Age: 29,
Expertise: ["Recruitment", "Employee Relations"], Expertise: ["Recruitment", "Employee Relations"],
DOB: "1994-09-09", DOB: "1994-09-09",
Email_id: "sneha.dutta@example.com", Email_id: "afan.shaikh@vought.com",
Contact: "9876543219", Contact: "9876543219",
Address: [{ PAddr: "1234, Street J, Ahmedabad", LAddr: "Gujarat" }] Address: [{ PAddr: "1234, Street J, Ahmedabad", LAddr: "Gujarat" }]
} }
@ -154,54 +155,63 @@ db.Employee.find({ Designation: "Programmer", Salary: { $gt: 30000 } })
2. Create a new document if no document contains `{Designation: "Tester", Company_name: "TCS", Age: 25}`: 2. Create a new document if no document contains `{Designation: "Tester", Company_name: "TCS", Age: 25}`:
```mongodb ```mongodb
db.Employee.update( db.Employee.update(
{ Designation: "Tester", Company_name: "TCS", Age: 25 }, { Designation: "Tester", Company_name: "Y-Space", 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" }] } }, { $setOnInsert: { Empid: 11, Name: { FName: "Anil", LName: "Salvi" }, Salary: 30000, DOB: "1998-01-01", Email_id: "anil.salvi@y-space.com", Contact: "9876543220", Address: [{ PAddr: "123 Street", LAddr: "Mumbai" }] } },
{ upsert: true } { upsert: true }
) )
``` ```
3. Select all documents where Age < 30 or Salary > 40000: 3. Select all documents where Age < 30 or Salary > 40000:
```mongodb ```mongodb
db.Employee.find({ $or: [{ Age: { $lt: 30 } }, { Salary: { $gt: 40000 } }] }) db.Employee.find({ $or: [{ Age: { $lt: 30 } }, { Salary: { $gt: 40000 } }] })
``` ```
4. Match documents where Address contains city "Pune" and Pin_code "411001": 4. Match documents where Address contains city "Pune" and Pin_code "411001":
```mongodb ```mongodb
db.Employee.find({ Address: { $elemMatch: { city: "Pune", Pin_code: "411001" } } }) db.Employee.find({ Address: { $elemMatch: { city: "Pune", Pin_code: "411001" } } })
``` ```
5. Find all documents with Company_name "TCS" and modify their salary by 2000: 5. Find all documents with Company_name "TCS" and modify their salary by 2000:
```mongodb ```mongodb
db.Employee.updateMany( db.Employee.updateMany(
{ Company_name: "TCS" }, { Company_name: "Oscorp" },
{ $inc: { Salary: 2000 } } { $inc: { Salary: 2000 } }
) )
``` ```
6. Find documents where Designation is not equal to "Developer": 6. Find documents where Designation is not equal to "Developer":
```mongodb ```mongodb
db.Employee.find({ Designation: { $ne: "Developer" } }) db.Employee.find({ Designation: { $ne: "Developer" } })
``` ```
7. Find _id, Designation, Address, and Name where Company_name is "Infosys": 7. Find _id, Designation, Address, and Name where Company_name is "Infosys":
```mongodb ```mongodb
db.Employee.find( db.Employee.find(
{ Company_name: "Infosys" }, { Company_name: "Wayne Industries" },
{ _id: 1, Designation: 1, Address: 1, Name: 1 } { _id: 1, Designation: 1, Address: 1, Name: 1 }
) )
``` ```
8. Select all documents where Designation is either "Developer" or "Tester": 8. Select all documents where Designation is either "Developer" or "Tester":
```mongodb ```mongodb
db.Employee.find({ Designation: { $in: ["Developer", "Tester"] } }) db.Employee.find({ Designation: { $in: ["Developer", "Tester"] } })
``` ```
9. Find all documents with exact match on Expertise array: 9. Find all documents with exact match on Expertise array:
```mongodb ```mongodb
db.Employee.find({ Expertise: { $all: ['Mongodb', 'Mysql', 'Cassandra'] } }) db.Employee.find({ Expertise: { $all: ['Mongodb', 'Mysql', 'Cassandra'] } })
``` ```
10. Drop single documents where Designation is "Developer": 10. Drop single documents where Designation is "Developer":
```mongodb ```mongodb
db.Employee.deleteMany({ Designation: "Developer" }) db.Employee.deleteMany({ Designation: "Developer" })
``` ```