practice3db
MONGODB PRACTICALS 2020-2021
SLIP SOLUTIONS
TABLE OF CONTENTS
SR.NO SLIP PAGE NO.
1 SLIP 11 2
2 SLIP 12 6
3 SLIP 13 10
4 SLIP 14 16
5 SLIP 15 20
6 SLIP 17 23
7 SLIP 18 26
8 SLIP 19 31
9 SLIP 20 38
SLIP 11
db.createCollection('Brands');
db.createCollection('Customers');
Brands
db.Brands.insert({"BName":"Bata","HQ":"Switzerland","Products":["Sports Shoes","Women'sShoes","MenShoes","School Shoes"]});
WriteResult({ "nInserted" : 1 })
db.Brands.insert({"BName":"Sparx","HQ":"NewDelhi","Products":["Sports Shoes","ComfortShoes","Flip Flops"]})
WriteResult({ "nInserted" : 1 })
db.Brands.insert({"BName":"Adidas","HQ":"Germany","Products":["Apparel","Footwear","Sports Equipments"]})
WriteResult({ "nInserted" : 1 })
db.Brands.insert({"BName":"Nike","HQ":"US","Products":["Athletic Footwear","Apparel","SportsEquipments"]})
WriteResult({ "nInserted" : 1 })
db.Brands.insert({"BName":"Reebok","HQ":"US","Products":["Sportswear","Footwear"]});
WriteResult({ "nInserted" : 1 })
Customer
db.Customers.insert({"CName":"Rohit","Product_Bought":"Flip Flops","Brand":"Sparx","Popularity":1})
WriteResult({ "nInserted" : 1 })
db.Customers.insert({"CName":"Pijush","Product_Bought":"Flip Flops","Brand":"Sparx","Popularity":0})
WriteResult({ "nInserted" : 1 })
db.Customers.insert({"CName":"Yogesh","Product_Bought":"Flip Flops","Brand":"Sparx","Popularity":2})
WriteResult({ "nInserted" : 1 })
db.Customers.insert({"CName":"Rasika","Product_Bought":"Apparel","Brand":"Adidas","Popularity":9})
WriteResult({ "nInserted" : 1 })
db.Customers.insert({"CName":"Gaurav","Product_Bought":"Sports Equipments","Brand":"Nike","Popularity":0})
WriteResult({ "nInserted" : 1 })
db.Customers.insert({"CName":"Anushka","Product_Bought":"Footwear","Brand":"Reebok","Popularity":10})
WriteResult({ "nInserted" : 1 })
db.Customers.insert({"CName":"Irfan","Product_Bought":"Men Shoes","Brand":"Bata","Popularity":5})
WriteResult({ "nInserted" : 1 })
QUERIES
db.Brands.distinct("BName");
Output :
[ "Adidas", "Bata", "Nike", "Reebok", "Sparx" ]
db.Brands.find({"BName":"Bata"},{Products:1}).pretty();
Output :
{
"_id" : ObjectId("60ab93bfc317a710b72f067b"),
"Products" : [
"Sports Shoes",
"Women's Shoes",
"Men Shoes",
"School Shoes"
]
}
db.Customers.find({"Product_Bought":"Flip Flops","Brand":"Sparx"},{CName:1}).pretty();
Output :
{ "_id" : ObjectId("60ab97f505821b9ec274c15a"), "CName" : "Rohit" }
{ "_id" : ObjectId("60ab97fd05821b9ec274c15b"), "CName" : "Pijush" }
{ "_id" : ObjectId("60ab980505821b9ec274c15c"), "CName" : "Yogesh" }
db.Customers.find({},{CName:1}).sort({Popularity:-1}).limit(1);
Output :
{ "_id" : ObjectId("60ab981e05821b9ec274c15f"), "CName" : "Anushka" }
SLIP 12
db.createCollection('Colleges');
db.createCollection('Professors');
Colleges
db.Colleges.insert({"Name":"Bytco College","Courses":["Arts","Science","Commedb.Colleges.insert({"Name":"Bytco College","Courses":["Arts","Science","Commerce"],"Professors":["Prof.Indrayani","Prof.Ashwini"]})
WriteResult({ "nInserted" : 1 })
db.Colleges.insert({"Name":"RYK College","Courses":["Arts","Science","Commerce","Engineering","Management"],"Professors":["Prof.Sneha","Prof.Deepali","Prof.Rutuja","Prof.Nivida","Prof.Priyanka"]})
WriteResult({ "nInserted" : 1 })
db.Colleges.insert({"Name":"BYK College","Courses":["Engineering","Management"],"Professors":["Prof.Shubham","Prof.Suraj"]})
WriteResult({ "nInserted" : 1 })
db.Colleges.insert({"Name":"K.KWagh College","Courses":["Engineering"],"Professors":["Prof.Pooja","Prof.Sayali"]})
WriteResult({ "nInserted" : 1 })
Professors
db.Professors.insert({"PName":"Prof.Indrayani","Age":26,"College":"Bytco College"})
WriteResult({ "nInserted" : 1 })
db.Professors.insert({"PName":"Prof.Ashwini","Age":24,"College":"Bytco College"})
WriteResult({ "nInserted" : 1 })
db.Professors.insert({"PName":"Prof.Sneha","Age":40,"College":"RYK College"})
WriteResult({ "nInserted" : 1 })
db.Professors.insert({"PName":"Prof.Deepali","Age":41,"College":"RYK College"})
WriteResult({ "nInserted" : 1 })
db.Professors.insert({"PName":"Prof.Rutuja","Age":35,"College":"RYK College"})
WriteResult({ "nInserted" : 1 })
db.Professors.insert({"PName":"Prof.Nivida","Age":45,"College":"RYK College"})
WriteResult({ "nInserted" : 1 })
db.Professors.insert({"PName":"Prof.Priyanka","Age":27,"College":"RYK College"})
WriteResult({ "nInserted" : 1 })
db.Professors.insert({"PName":"Prof.Suraj","Age":48,"College":"BYK College"})
WriteResult({ "nInserted" : 1 })
db.Professors.insert({"PName":"Prof.Shubham","Age":70,"College":"BYK College"})
WriteResult({ "nInserted" : 1 })
db.Professors.insert({"PName":"Prof.Sayali","Age":24,"College":"K.K Wagh College"})
WriteResult({ "nInserted" : 1 })
db.Professors.insert({"PName":"Prof.Pooja","Age":22,"College":"K.K Wagh College"})
WriteResult({ "nInserted" : 1 })
QUERIES
db.Colleges.find({"Courses":"Arts"},{Name:1}).pretty();
Output :
{ "_id" : ObjectId("60ab9e7105821b9ec274c161"), "Name" : "Bytco College" }
{ "_id" : ObjectId("60ab9e8f05821b9ec274c162"), "Name" : "RYK College" }
db.Professors.find({Age:{$gt:40}},{"PName":1}).pretty();
Output :
{ "_id" : ObjectId("60aba0b105821b9ec274c168"), "PName" : "Prof.Deepali" }
{ "_id" : ObjectId("60aba0bd05821b9ec274c16a"), "PName" : "Prof.Nivida" }
{ "_id" : ObjectId("60aba0c905821b9ec274c16c"), "PName" : "Prof.Suraj" }
{ "_id" : ObjectId("60aba0d005821b9ec274c16d"), "PName" : "Prof.Shubham" }
db.Colleges.find({"Professors":{$size:5}},{Name:1}).pretty();
Output :
{ "_id" : ObjectId("60ab9e8f05821b9ec274c162"), "Name" : "RYK College" }
db.Colleges.find({$where:"this.Courses.length>2"},{Name:1}).pretty();
Output :
{ "_id" : ObjectId("60ab9e7105821b9ec274c161"), "Name" : "Bytco College" }
{ "_id" : ObjectId("60ab9e8f05821b9ec274c162"), "Name" : "RYK College" }
SLIP 13
db.createCollection('Product');
{ "ok" : 1 }
db.createCollection('Order');
{ "ok" : 1 }
Product
db.Product.insert({"PId":"P101","PName":"Cycle","Price":5000})
WriteResult({ "nInserted" : 1 })
db.Product.insert({"PId":"P102","PName":"Shoes","Price":1200})
WriteResult({ "nInserted" : 1 })
db.Product.insert({"PId":"P103","PName":"Table","Price":3000})
WriteResult({ "nInserted" : 1 })
db.Product.insert({"PId":"P104","PName":"Speakers","Price":2500})
WriteResult({ "nInserted" : 1 })
db.Product.insert({"PId":"P105","PName":"NVIDIA Graphic Card","Price":15999})
WriteResult({ "nInserted" : 1 })
Order
db.Order.insert({"OrderNo":"O101","Customer":"Pijush","Product":{"Name":"NVIDIA Graphic Card","Price":15999},"Date":2021-12-3,"Status":"In Process","Bill":16000})
WriteResult({ "nInserted" : 1 })
db.Order.insert({"OrderNo":"O102","Customer":"Yogesh","Product":{"Name":"Shoes","Price":1200},"Date":2017-4-2,"Status":"Processed","Bill":1250,"Invoice":{"InvoiceNo":"N1234","Bill":1250,"Date":2017-4-2}})
WriteResult({ "nInserted" : 1 })
db.Order.insert({"OrderNo":"O103","Customer":"Rohit","Product":{"Name":"Cycle","Price":5000},"Date":2009-12-7,"Status":"In Process","Bill":6000})
WriteResult({ "nInserted" : 1 })
db.Order.insert({"OrderNo":"O104","Customer":"Vivek","Product":{"Name":"Speakers","Price":2500},"Date":2016-11-1,"Status":"Processed","Bill":3000,"Invoice":{"InvoiceNo":"N1235","Bill":3500,"Date":2016-11-1}})
WriteResult({ "nInserted" : 1 })
db.Order.insert({"OrderNo":"O103","Customer":"Sachin","Product":{"Name":"Table","Price":3000},"Date":2008-10-7,"Status":"In Process","Bill":4000})
WriteResult({ "nInserted" : 1 })
QUERIES
db.Product.find().pretty()
Output :
{
"_id" : ObjectId("60aba88305821b9ec274c170"),
"PId" : "P101",
"PName" : "Cycle",
"Price" : 5000
}
{
"_id" : ObjectId("60aba88905821b9ec274c171"),
"PId" : "P102",
"PName" : "Shoes",
"Price" : 1200
}
{
"_id" : ObjectId("60aba88e05821b9ec274c172"),
"PId" : "P103",
"PName" : "Table",
"Price" : 3000
}
{
"_id" : ObjectId("60aba89305821b9ec274c173"),
"PId" : "P104",
"PName" : "Speakers",
"Price" : 2500
}
{
"_id" : ObjectId("60aba89905821b9ec274c174"),
"PId" : "P105",
"PName" : "NVIDIA Graphic Card",
"Price" : 15999
}
db.Order.find({Bill:{$gt:10000}}).pretty()
Output :
{
"_id" : ObjectId("60abac8905821b9ec274c175"),
"OrderNo" : "O101",
"Customer" : "Pijush",
"Product" : {
"Name" : "NVIDIA Graphic Card",
"Price" : 15999
},
"Date" : 2006,
"Status" : "In Process",
"Bill" : 16000
}
db.Order.find({Status:"In Process"}).pretty()
Output :
{
"_id" : ObjectId("60abac8905821b9ec274c175"),
"OrderNo" : "O101",
"Customer" : "Pijush",
"Product" : {
"Name" : "NVIDIA Graphic Card",
"Price" : 15999
},
"Date" : 2006,
"Status" : "In Process",
"Bill" : 16000
}
{
"_id" : ObjectId("60abacbf05821b9ec274c177"),
"OrderNo" : "O103",
"Customer" : "Rohit",
"Product" : {
"Name" : "Cycle",
"Price" : 5000
},
"Date" : 1990,
"Status" : "In Process",
"Bill" : 6000
}
{
"_id" : ObjectId("60abacd405821b9ec274c179"),
"OrderNo" : "O103",
"Customer" : "Sachin",
"Product" : {
"Name" : "Table",
"Price" : 3000
},
"Date" : 1991,
"Status" : "In Process",
"Bill" : 4000
}
db.Order.find({Customer:"Vivek",Status:"Processed"},{OrderNo:1,Product:1,Date:1,Bill:1,Invoice:1}).pretty();
Output :
{
"_id" : ObjectId("60abacc605821b9ec274c178"),
"OrderNo" : "O104",
"Product" : {
"Name" : "Speakers",
"Price" : 2500
},
"Date" : 2004,
"Bill" : 3000,
"Invoice" : {
"InvoiceNo" : "N1235",
"Bill" : 3500,
"Date" : 2004
}
}
SLIP 14
db.createCollection('WorldCups')
{ "ok" : 1 }
db.createCollection('Players')
{ "ok" : 1 }
WorldCups
db.WorldCups.insert({"WorldCup":"FIFA World Cup","Year":2015,"Events":["Men's Football","Women's Football"]})
WriteResult({ "nInserted" : 1 })
db.WorldCups.insert({"WorldCup":"Swimming World Cup","Year":2013,"Events":["100m freestyle","200m Freestyle"]})
WriteResult({ "nInserted" : 1 })
db.WorldCups.insert({"WorldCup":"Cricket World Cup","Year":2019,"Events":["One-Day Cricket","T-20 Cricket"]})
WriteResult({ "nInserted" : 1 })
db.WorldCups.insert({"WorldCup":"Shooting World Cup","Year":2018,"Events":["Rapid Pistol","Shotgun"]})
WriteResult({ "nInserted" : 1 })
db.WorldCups.insert({"WorldCup":"Cycling World Cup","Year":2018,"Events":["Rock Cycling","BMX"]})
WriteResult({ "nInserted" : 1 })
Players
db.Players.insert({Name:"Sachin",Age:26,WorldCup:"Cricket World Cup",Year:2019})
WriteResult({ "nInserted" : 1 })
db.Players.insert({Name:"Rasika",Age:23,WorldCup:"Shooting World Cup",Year:2013})
WriteResult({ "nInserted" : 1 })
db.Players.insert({Name:"Vaishnavi",Age:24,WorldCup:"Cycling World Cup",Year:2015})
WriteResult({ "nInserted" : 1 })
db.Players.insert({Name:"Vivek",Age:21,WorldCup:"Cricket World Cup",Year:2019})
WriteResult({ "nInserted" : 1 })
db.Players.insert({Name:"Rameshwar",Age:28,WorldCup:"Football World Cup",Year:2015})
WriteResult({ "nInserted" : 1 })
db.Players.insert({Name:"Bittu",Age:26,WorldCup:"Football World Cup",Year:2018})
WriteResult({ "nInserted" : 1 })
db.Players.insert({Name:"Gayatri",Age:20,WorldCup:"Cricket World Cup",Year:2018})
WriteResult({ "nInserted" : 1 })
QUERIES
db.WorldCups.find({Year:2018}).pretty()
Output :
{
"_id" : ObjectId("60abbc6205821b9ec274c17d"),
"WorldCup" : "Shooting World Cup",
"Year" : 2018,
"Events" : [
"Rapid Pistol",
"Shotgun"
]
}
{
"_id" : ObjectId("60abbc6905821b9ec274c17e"),
"WorldCup" : "Cycling World Cup",
"Year" : 2018,
"Events" : [
"Rock Cycling",
"BMX"
]
}
db.Players.find({Age:{$gt:23}},{Name:1}).pretty()
Output :
{ "_id" : ObjectId("60abbca605821b9ec274c17f"), "Name" : "Sachin" }
{ "_id" : ObjectId("60abbcbb05821b9ec274c181"), "Name" : "Vaishnavi" }
{ "_id" : ObjectId("60abbcc805821b9ec274c183"), "Name" : "Rameshwar" }
{ "_id" : ObjectId("60abbcd005821b9ec274c184"), "Name" : "Bittu" }
db.WorldCups.find({WorldCup:"FIFA World Cup",Year:2015},{Events:1}).pretty()
Output :
{
"_id" : ObjectId("60abbc3105821b9ec274c17a"),
"Events" : [
"Men's Football",
"Women's Football"
]
}
db.Players.find({WorldCup:"Cricket World Cup",Year:2019},{Name:1}).pretty()
Output :
{ "_id" : ObjectId("60abbca605821b9ec274c17f"), "Name" : "Sachin" }
{ "_id" : ObjectId("60abbcc205821b9ec274c182"), "Name" : "Vivek" }
SLIP 15
db.createCollection('Olympic')
{ "ok" : 1 }
db.createCollection('Country')
{ "ok" : 1 }
Olympics
db.Olympics.insert({"Name":"Tokyo Olympics",Year:2020,Games:["Shooting","Badminton","Swimming"]})
WriteResult({ "nInserted" : 1 })
db.Olympics.insert({"Name":"Nagano Olympics",Year:2015,Games:["Shooting","Badminton","Gymnastics","Swimming","Cricket","Boating"]})
WriteResult({ "nInserted" : 1 })
db.Olympics.insert({"Name":"Rio Olympics",Year:2010,Games:["Gymnastics","Badminton","Swimming"]})
WriteResult({ "nInserted" : 1 })
db.Olympics.insert({"Name":"Beijing Olympics",Year:2008,Games:["Shooting","Football","Swimming"]})
WriteResult({ "nInserted" : 1 })
db.Olympics.insert({"Name":"London Olympics",Year:2000,Games:["Cycling","Badminton","Swimming"]})
WriteResult({ "nInserted" : 1 })
Countries
db.Countries.insert({"Name":"USA",Olympics:"Nagano Olympics",Year:2015,Games:["Badminton"],Medal:"Silver"})
WriteResult({ "nInserted" : 1 })
db.Countries.insert({"Name":"India",Olympics:"Nagano Olympics",Year:2015,Games:["Badminton"],Medal:"Silver"})
WriteResult({ "nInserted" : 1 })
db.Countries.insert({"Name":"Japan",Olympics:"Nagano Olympics",Year:2015,Games:["Badminton"],Medal:"Silver"})
WriteResult({ "nInserted" : 1 })
db.Countries.insert({"Name":"Brazil",Olympics:"Nagano Olympics",Year:2015,Games:["Badminton","Gymnastics","Swimming","Cricket","Boating","Shooting"],Medal:"Gold"})
WriteResult({ "nInserted" : 1 })
db.Countries.insert({"Name":"India",Olympics:"London Olympics",Year:2000,Games:["Swimming"],Medal:"Bronze"})
WriteResult({ "nInserted" : 1 })
db.Countries.insert({"Name":"China",Olympics:"London Olympics",Year:2015,Games:["Swimming"],Medal:"Bronze"})
WriteResult({ "nInserted" : 1 })
db.Countries.insert({"Name":"SouthKorea",Olympics:"Rio Olympics",Year:2010,Games:["Shooting"],Medal:"Gold"})
WriteResult({ "nInserted" : 1 })
OUERIES
1.
db.Olympics.find({Year:2008},{Games:1}).pretty();
Output :
{
"_id" : ObjectId("60abc62a05821b9ec274c189"),
"Games" : [
"Shooting",
"Football",
"Swimming"
]
}
db.Countries.find({Year:2015},{Name:1}).sort({Name:1}).pretty();
Output :
{ "_id" : ObjectId("60abc66d05821b9ec274c18e"), "Name" : "Brazil" }
{ "_id" : ObjectId("60abc81e05821b9ec274c190"), "Name" : "China" }
{ "_id" : ObjectId("60abc65d05821b9ec274c18c"), "Name" : "India" }
{ "_id" : ObjectId("60abc66405821b9ec274c18d"), "Name" : "Japan" }
{ "_id" : ObjectId("60abc65605821b9ec274c18b"), "Name" : "USA" }
db.Countries.find({$where:"this.Games.length>2",Year:2015},{Name:1}).pretty();
Output :
{ "_id" : ObjectId("60abc66d05821b9ec274c18e"), "Name" : "Brazil" }
db.Countries.find({Medal:"Silver",Games:"Badminton",Year:2015},{Name:1}).pretty()
Output :
{ "_id" : ObjectId("60abc65605821b9ec274c18b"), "Name" : "USA" }
{ "_id" : ObjectId("60abc65d05821b9ec274c18c"), "Name" : "India" }
{ "_id" : ObjectId("60abc66405821b9ec274c18d"), "Name" : "Japan" }
SLIP 17
db.createCollection('Mobile')
{ "ok" : 1 }
db.createCollection('Customer')
{ "ok" : 1 }
Mobile
db.Mobile.insert({"Brand":"iPhone 7 Plus",RAM:"3GB",ROM:"32GB",Rating:10})
WriteResult({ "nInserted" : 1 })
db.Mobile.insert({"Brand":"Oppo K3",RAM:"3GB",ROM:"32GB",Rating:10})
WriteResult({ "nInserted" : 1 })
db.Mobile.insert({"Brand":"OnePlus 5",RAM:"12GB",ROM:"64GB",Rating:9})
WriteResult({ "nInserted" : 1 })
db.Mobile.insert({"Brand":"Samsung J6",RAM:"6GB",ROM:"64GB",Rating:7})
WriteResult({ "nInserted" : 1 })
db.Mobile.insert({"Brand":"Nokia",RAM:"2GB",ROM:"16GB",Rating:5})
WriteResult({ "nInserted" : 1 })
Customer
db.Customer.insert({"Name":"Pratiksha","Phone":"iPhone 7 Plus"})
WriteResult({ "nInserted" : 1 })
db.Customer.insert({"Name":"Anushka","Phone":"iPhone 7 Plus"})
WriteResult({ "nInserted" : 1 })
db.Customer.insert({"Name":"Rasika","Phone":"iPhone 7 Plus"})
WriteResult({ "nInserted" : 1 })
db.Customer.insert({"Name":"Pushpa","Phone":"Samsung J6"})
WriteResult({ "nInserted" : 1 })
db.Customer.insert({"Name":"Sayali","Phone":"Samsung J6"})
WriteResult({ "nInserted" : 1 })
db.Customer.insert({"Name":"Pijush","Phone":"Nokia"})
WriteResult({ "nInserted" : 1 })
db.Customer.insert({"Name":"Rohit","Phone":"OnePlus 5"})
WriteResult({ "nInserted" : 1 })
db.Customer.insert({"Name":"Yogesh","Phone":"Oppo K3"})
WriteResult({ "nInserted" : 1 })
QUERIES
db.Mobile.find({RAM:"3GB",ROM:"32GB"},{Brand:1}).pretty()
Output :
{ "_id" : ObjectId("60abd24605821b9ec274c192"), "Brand" : "iPhone 7 Plus" }
{ "_id" : ObjectId("60abd25105821b9ec274c193"), "Brand" : "Oppo K3" }
db.Customer.find({Phone:"Samsung J6"},{Name:1}).pretty()
Output :
{ "_id" : ObjectId("60abd2ac05821b9ec274c19a"), "Name" : "Pushpa" }
{ "_id" : ObjectId("60abd2b105821b9ec274c19b"), "Name" : "Sayali" }
db.Mobile.distinct("Brand")
Output :
[ "Nokia", "OnePlus 5", "Oppo K3", "Samsung J6", "iPhone 7 Plus" ]
db.Mobile.find({Rating:10},{Brand:1})
Output :
{ "_id" : ObjectId("60abd24605821b9ec274c192"), "Brand" : "iPhone 7 Plus" }
{ "_id" : ObjectId("60abd25105821b9ec274c193"), "Brand" : "Oppo K3" }
db.Customer.find({Phone:"iPhone 7 Plus"},{Name:1}).sort({Name:1})
Output :
{ "_id" : ObjectId("60abd2a105821b9ec274c198"), "Name" : "Anushka" }
{ "_id" : ObjectId("60abd29c05821b9ec274c197"), "Name" : "Pratiksha" }
{ "_id" : ObjectId("60abd2a605821b9ec274c199"), "Name" : "Rasika" }
SLIP 18
db.createCollection("TourismIndustry")
{ "ok" : 1 }
db.createCollection("Tour")
{ "ok" : 1 }
TourismIndustry
db.TourismIndustry.insert({Name:"Veena World",Rating:10,Package:[{Name:"Shillong",Cost:10000},{Name:"Gujarat",Cost:7000},{Name:"Karnataka",Cost:6000}]})
WriteResult({ "nInserted" : 1 })
db.TourismIndustry.insert({Name:"World Caravan",Rating:8,Package:[{Name:"Shillong",Cost:9000},{Name:"Puducherry",Cost:8000},{Name:"Kerala",Cost:10000}]})
WriteResult({ "nInserted" : 1 })
db.TourismIndustry.insert({Name:"Kesari",Rating:9,Package:[{Name:"Manali",Cost:7000},{Name:"Shimla",Cost:9000},{Name:"Kerala",Cost:11000}]})
WriteResult({ "nInserted" : 1 })
Tour
db.Tour.insert({Source:"Kerala",Destination:"Shillong",TourismIndustry:"Veena World",Expenses:20000,Year:2018,Customer:[{CName:"John"},{CName:"Abhijeet"},{CName:"Manisha"},{CName:"Mansi"}]})
WriteResult({ "nInserted" : 1 })
db.Tour.insert({Source:"Manali",Destination:"Shillong",TourismIndustry:"Veena World",Expenses:30000,Year:2016,Customer:[{CName:"John"},{CName:"Rasika"},{CName:"Pratiksha"},{CName:"Gauri"}]})
WriteResult({ "nInserted" : 1 })
db.Tour.insert({Source:"Shimla",Destination:"Karnataka",TourismIndustry:"World Caravan",Expenses:15000,Year:2012,Customer:[{CName:"John"},{CName:"Deepali"},{CName:"Priyanka"},{CName:"Sukanya"}]})
WriteResult({ "nInserted" : 1 })
db.Tour.insert({Source:"Kerala",Destination:"Dehradun",TourismIndustry:"Kesari",Expenses:17000,Year:2020,Customer:[{CName:"Pijush"},{CName:"Yogesh"},{CName:"Rohit"}]})
WriteResult({ "nInserted" : 1 })
QUERIES
db.TourismIndustry.find({Name:"Kesari"},{Package:1}).pretty()
Output :
{
"_id" : ObjectId("60abdd9005821b9ec274c1a1"),
"Package" : [
{
"Name" : "Manali",
"Cost" : 7000
},
{
"Name" : "Shimla",
"Cost" : 9000
},
{
"Name" : "Kerala",
"Cost" : 11000
}
]
}
db.TourismIndustry.find({Rating:10},{Name:1}).pretty()
Output :
{ "_id" : ObjectId("60abdd6405821b9ec274c19f"), "Name" : "Veena World" }
db.Tour.find({"Customer.CName":"John"},{Expenses:1}).sort({Year:1}).limit(3).pretty()
Output :
{ "_id" : ObjectId("60abddca05821b9ec274c1a4"), "Expenses" : 15000 }
{ "_id" : ObjectId("60abddc105821b9ec274c1a3"), "Expenses" : 30000 }
{ "_id" : ObjectId("60abddbb05821b9ec274c1a2"), "Expenses" : 20000 }
db.Tour.aggregate([{"sort":{"year":1}},{"limit":3},{group:{_id:null,"count":{"sum":"$Expenses"}}}])
{ "_id" : null, "count" : 65000 }
db.Tour.find({"Destination":"Shillong"},{"Customer.CName":1}).pretty()
Output :
{
"_id" : ObjectId("60abddbb05821b9ec274c1a2"),
"Customer" : [
{
"CName" : "John"
},
{
"CName" : "Abhijeet"
},
{
"CName" : "Manisha"
},
{
"CName" : "Mansi"
}
]
}
{
"_id" : ObjectId("60abddc105821b9ec274c1a3"),
"Customer" : [
{
"CName" : "John"
},
{
"CName" : "Rasika"
},
{
"CName" : "Pratiksha"
},
{
"CName" : "Gauri"
}
]
}
SLIP 19
db.createCollection('Brands')
{ "ok" : 1 }
db.createCollection('Customer')
{ "ok" : 1 }
Brands
db.Brands.insert({"Brand":"Lakme","Products":[{"PName":"Lipstick",Color:"Hot Pink","ExpiryDate":"03-10-2018",Rating:7},{"PName":"Eyeconic Kajal",Color:"Black","ExpiryDate":"06-11-2019",Rating:10}]})
WriteResult({ "nInserted" : 1 })
db.Brands.insert({"Brand":"Maybelline","Products":[{"PName":"Baby Lips",Color:"Nude","ExpiryDate":"30-11-2019",Rating:10},{"PName":"Nail Paint",Color:"Lavender","ExpiryDate":"02-09-2020",Rating:6}]})
WriteResult({ "nInserted" : 1 })
db.Brands.insert({"Brand":"Kylie Cosmetics","Products":[{"PName":"Lipliner",Color:"Maroon","ExpiryDate":"30-11-2019",Rating:3},{"PName":"Royal Peach Palette",Color:"Multi-coloured","ExpiryDate":"30-11-2019",Rating:9}]})
WriteResult({ "nInserted" : 1 })
db.Brands.insert({"Brand":"RareBeauty","Products":[{"PName":"Liquid Blush",Color:"Baby Pink","ExpiryDate":"03-10-2018",Rating:10},{"PName":"Foundation",Color:"Nude","ExpiryDate":"05-07-2021",Rating:3}]})
WriteResult({ "nInserted" : 1 })
Customers
db.Customer.insert({"Name":"Taylor Swift",Shopping:[{"Brand":"Lakme","Product":"Lipstick"},{"Brand":"Rare Beauty","Product":"Liquid Blush"}]})
WriteResult({ "nInserted" : 1 })
db.Customer.insert({"Name":"Halsey",Shopping:[{"Brand":"Kylie Cosmetics","Product":"Lipliner"},{"Brand":"Maybelline","Product":"Nail Paint"}]})
WriteResult({ "nInserted" : 1 })
db.Customer.insert({"Name":"SelenaGomez",Shopping:[{"Brand":"Maybelline","Product":"Baby Lips"},{"Brand":"Maybelline","Product":"Nail Paint"}]})
WriteResult({ "nInserted" : 1 })
db.Customer.insert({"Name":"Taylor Swift",Shopping:[{"Brand":"Lakme","Product":"Lipstick"},{"Brand":"Lakme","Product":"Eyeconic Kajal"}]})
WriteResult({ "nInserted" : 1 })
db.Customer.insert({"Name":"Lana Del Rey",Shopping:[{"Brand":"Lakme","Product":"Lipstick"},{"Brand":"Maybelline","Product":"Nail Paint"}]})
WriteResult({ "nInserted" : 1 })
QUERIES
1.
db.Brands.find({"Products.ExpiryDate":"30-11-2019"},{"Products.PName":1}).pretty()
Output :
{
"_id" : ObjectId("60abe70605821b9ec274c1a7"),
"Products" : [
{
"PName" : "Baby Lips"
},
{
"PName" : "Nail Paint"
}
]
}
{
"_id" : ObjectId("60abe71105821b9ec274c1a8"),
"Products" : [
{
"PName" : "Lipliner"
},
{
"PName" : "Royal Peach Palette"
}
]
}
db.Brands.find({"Products.Rating":{$gt:3}},{"Brand":1,"Products.PName":1}).pretty()
Output :
{
"_id" : ObjectId("60abe6e705821b9ec274c1a6"),
"Brand" : "Lakme",
"Products" : [
{
"PName" : "Lipstick"
},
{
"PName" : "Eyeconic Kajal"
}
]
}
{
"_id" : ObjectId("60abe70605821b9ec274c1a7"),
"Brand" : "Maybelline",
"Products" : [
{
"PName" : "Baby Lips"
},
{
"PName" : "Nail Paint"
}
]
}
{
"_id" : ObjectId("60abe71105821b9ec274c1a8"),
"Brand" : "Kylie Cosmetics",
"Products" : [
{
"PName" : "Lipliner"
},
{
"PName" : "Royal Peach Palette"
}
]
}
{
"_id" : ObjectId("60abe71a05821b9ec274c1a9"),
"Brand" : "Rare Beauty",
"Products" : [
{
"PName" : "Liquid Blush"
},
{
"PName" : "Foundation"
}
]
}
db.Customer.find({$and:[{"Shopping.Brand":"Lakme","Shopping.Product":"Lipstick"},{"Shopping.Brand":"Maybelline","Shopping.Product":"Nail Paint"}]},{Name:1}).pretty()
Output :
{ "_id" : ObjectId("60abeb9605821b9ec274c1ae"), "Name" : "Lana Del Rey" }
db.Brands.find({},{Brand:1,"Products.PName":1}).pretty()
Output :
{
"_id" : ObjectId("60abe6e705821b9ec274c1a6"),
"Brand" : "Lakme",
"Products" : [
{
"PName" : "Lipstick"
},
{
"PName" : "Eyeconic Kajal"
}
]
}
{
"_id" : ObjectId("60abe70605821b9ec274c1a7"),
"Brand" : "Maybelline",
"Products" : [
{
"PName" : "Baby Lips"
},
{
"PName" : "Nail Paint"
}
]
}
{
"_id" : ObjectId("60abe71105821b9ec274c1a8"),
"Brand" : "Kylie Cosmetics",
"Products" : [
{
"PName" : "Lipliner"
},
{
"PName" : "Royal Peach Palette"
}
]
}
{
"_id" : ObjectId("60abe71a05821b9ec274c1a9"),
"Brand" : "Rare Beauty",
"Products" : [
{
"PName" : "Liquid Blush"
},
{
"PName" : "Foundation"
}
]
}
SLIP 20
db.createCollection('Hospital')
{ "ok" : 1 }
Hospital
db.Hospital.insert({Hno:1,Hname:"Jehangir Hospital",Specialization:["Pediatric","Gynaec","Orthopaedic"],People:[{Pname:"Gaurav",Rating:4},{Pname:"Rameshwar",Rating:2},{Pname:"Siya",Rating:3}],Doctor:[{"Dname" : "Dr.Vishal","Visit" : "Sunday"},{"Dname" : "Dr.Neha","Visit" : "Sunday"}]})
WriteResult({ "nInserted" : 1 })
db.Hospital.insert({Hno:2,Hname:"Bytco Hospital",Specialization:["Pediatric","Gynaec"],People:[{Pname:"Pooja",Rating:8},{Pname:"Pushpa",Rating:6}],Doctor:[{"Dname" : "Dr.Vidhi","Visit" : "Tuesday"}]})
WriteResult({ "nInserted" : 1 })
db.Hospital.insert({Hno:3,Hname:"Apollo Hospital",Specialization:["Gynaec"],People:[{Pname:"Priyanka",Rating:9},{Pname:"Sukanya",Rating:7}],Doctor:[{"Dname" : "Dr.Vaani","Visit" : "Wednesday"}]})
WriteResult({ "nInserted" : 1 })
QUERIES
1.
db.Hospital.find({Specialization:"Pediatric"},{Hname:1}).pretty()
Output :
{
"_id" : ObjectId("60abf3b105821b9ec274c1af"),
"Hname" : "Jehangir Hospital"
}
{ "_id" : ObjectId("60abf3b805821b9ec274c1b0"), "Hname" : "Bytco Hospital" }
db.Hospital.find({Hname:"Jehangir Hospital","Doctor.Visit":"Sunday"},{"Doctor.Dname":1}).pretty()
Output :
{
"_id" : ObjectId("60abf3b105821b9ec274c1af"),
"Doctor" : [
{
"Dname" : "Dr.Vishal"
},
{
"Dname" : "Dr.Neha"
}
]
}
db.Hospital.find({Specialization:"Gynaec"},{Hname:1}).sort({"People.Rating":-1}).limit(1)
Output :
{ "_id" : ObjectId("60abf3bf05821b9ec274c1b1"), "Hname" : "Apollo Hospital" }
db.Hospital.find({Hname:"JehangirHospital","People.Rating":{$gte:3}},{"People.Pname":1}).pretty();
Output :
{
"_id" : ObjectId("60abf3b105821b9ec274c1af"),
"People" : [
{
"Pname" : "Gaurav"
},
{
"Pname" : "Rameshwar"
},
{
"Pname" : "Siya"
}
]
}