Basic Exercise MongoDB
Basic Exercise MongoDB
Agenda: Create database, Create collection, insert data, find, find one, sort, limit, skip, distinct,
projection.
Create a student database with the fields: (SRN, Sname, Degree, Sem, CGPA)
> doc1=({srn:110,sname:"Rahul",degree:"BCA",sem:6,CGPA:7.9})
{
"srn" : 110,
"sname" : "Rahul",
"degree" : "BCA",
"sem" : 6,
"CGPA" : 7.9
}
> db.stud09.insert(doc1)
Questions:
1.display all the documents
studb9> db.stud09.find()
[
{
_id: ObjectId('6672e34374ce5d4edb2a7c4d'),
srn: 110,
sname: 'Rahul',
degree: 'BCA',
sem: 6,
CGPA: 7.9
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c4e'),
srn: 111,
sname: 'Anita',
degree: 'BSc',
sem: 6,
CGPA: 8.2
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c4f'),
srn: 112,
sname: 'Vikas',
degree: 'BCom',
sem: 6,
CGPA: 7.5
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c50'),
srn: 113,
sname: 'Sneha',
degree: 'BA',
sem: 6,
CGPA: 8
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c51'),
srn: 114,
sname: 'Raj',
degree: 'BBA',
sem: 6,
CGPA: 7.3
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c52'),
srn: 115,
sname: 'Pooja',
degree: 'BCA',
sem: 6,
CGPA: 7.7
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c53'),
srn: 116,
sname: 'Amit',
degree: 'BSc',
sem: 6,
CGPA: 8.4
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c54'),
srn: 117,
sname: 'Kiran',
degree: 'BCom',
sem: 6,
CGPA: 7.8
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c55'),
srn: 118,
sname: 'Ravi',
degree: 'BA',
sem: 6,
CGPA: 7.6
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c56'), srn: 119,
sname: 'Priya',
degree: 'BBA',
sem: 6,
CGPA: 8.1
}
]
2.Display all the students in BCA
db.stud09.find({degree:"BCA"})
[
{
_id: ObjectId('6672e34374ce5d4edb2a7c4d'),
srn: 110,
sname: 'Rahul',
degree: 'BCA',
sem: 6,
CGPA: 7.9
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c52'),
srn: 115,
sname: 'Pooja',
degree: 'BCA',
sem: 6,
CGPA: 7.7
}
]
db.stud09.find().sort({sname:1}).limit(5)
[
{
_id: ObjectId('6672e34374ce5d4edb2a7c53'),
srn: 116,
sname: 'Amit',
degree: 'BSc',
sem: 6,
CGPA: 8.4
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c4e'),
srn: 111,
sname: 'Anita',
degree: 'BSc',
sem: 6,
CGPA: 8.2
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c54'),
srn: 117,
sname: 'Kiran',
degree: 'BCom',
sem: 6,
CGPA: 7.8
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c52'),
srn: 115,
sname: 'Pooja',
degree: 'BCA',
sem: 6,
CGPA: 7.7
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c56'),
srn: 119,
sname: 'Priya',
degree: 'BBA',
sem: 6,
CGPA: 8.1
}
]
5.display students 5,6,7
db.stud09.find().sort({sname:1}).skip(4).limit(3)
[
{
_id: ObjectId('6672e34374ce5d4edb2a7c56'),
srn: 119,
sname: 'Priya',
degree: 'BBA',
sem: 6,
CGPA: 8.1
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c4d'),
srn: 110,
sname: 'Rahul',
degree: 'BCA',
sem: 6,
CGPA: 7.9
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c51'),
srn: 114,
sname: 'Raj',
degree: 'BBA',
sem: 6,
CGPA: 7.3
}
]
db.stud09.find({sname:"Rahul"},{degree:1,_id:0})
[ { degree: 'BCA' } ]
db.stud09.countDocuments({degree:"BCA"})
2
db.stud09.find({},{degree:1,_id:0})
[
{ degree: 'BCA' },
{ degree: 'BSc' },
{ degree: 'BCom' },
{ degree: 'BA' },
{ degree: 'BBA' },
{ degree: 'BCA' },
{ degree: 'BSc' },
{ degree: 'BCom' },
{ degree: 'BA' },
{ degree: 'BBA' }
11.Display all the BCA students with CGPA greater than 6, but less than 7.5
db.stud09.find({degree:"BCA",CGPA:{$gt:6,$lt:7.5}})
[
{
_id: ObjectId('6672e34374ce5d4edb2a7c4d'),
srn: 110,
sname: 'Rahul',
degree: 'BCA',
sem: 6,
CGPA: 6.5
}
]
Create an employee database with the fields: {eid, ename, dept, desig, salary, yoj, address{dno,
street, locality, city}}
Questions:
1.Display all the employees with salary in range (50000, 75000)
db.emp.find({salary:{$gt:50000,$lt:75000}})
[
{
_id: ObjectId('6673fad96cc1a0e2a1b04e19'),
eid: 3,
ename: 'John',
dept: 'IT',
desig: 'system analyst',
salary: 60000,
yoj: 2016,
address: {
dno: 789,
street: 8,
locality: 'Tech Hub',
city: 'San Francisco'
}
},
{
_id: ObjectId('6673fad96cc1a0e2a1b04e1b'),
eid: 5,
ename: 'Michael',
dept: 'Finance',
desig: 'financial analyst',
salary: 55000,
yoj: 2019,
address: {
dno: 567,
street: 7,
locality: 'Financial District',
city: 'Chicago'
}
},
{
_id: ObjectId('6673fad96cc1a0e2a1b04e1d'),
eid: 7,
ename: 'Daniel',
dept: 'IT',
desig: 'software engineer',
salary: 65000,
yoj: 2016,
address: { dno: 890, street: 6, locality: 'Tech Park', city:
'Seattle' }
},
{
_id: ObjectId('6673fad96cc1a0e2a1b04e1f'),
eid: 9,
ename: 'Jacob',
dept: 'Finance',
desig: 'financial planner',
salary: 58000,
yoj: 2019,
address: { dno: 345, street: 1, locality: 'Finance Lane', city:
'Atlanta' }
},
{
_id: ObjectId('6673fad96cc1a0e2a1b04e20'),
eid: 10,
ename: 'Isabella',
dept: 'Marketing',
desig: 'marketing strategist',
salary: 52000,
yoj: 2018,
address: {
dno: 789,
street: 2,
locality: 'Marketing Square',
city: 'Miami'
}
},
{
_id: ObjectId('6673fad96cc1a0e2a1b04e21'),
eid: 11,
ename: 'Ethan',
dept: 'IT',
desig: 'database administrator',
salary: 70000,
yoj: 2016,
address: {
dno: 456,
street: 3,
locality: 'Database Lane',
city: 'San Diego'
}
}
]
db.emp.find({desig:"developer"})
[
{
_id: ObjectId('6673fad96cc1a0e2a1b04e19'),
eid: 3,
ename: 'John',
dept: 'IT',
desig: 'developer',
salary: 60000,
yoj: 2016,
address: {
dno: 789,
street: 8,
locality: 'Tech Hub',
city: 'San Francisco'
}
},
{
_id: ObjectId('6673fad96cc1a0e2a1b04e21'),
eid: 11,
ename: 'Ethan',
dept: 'IT',
desig: 'developer',
salary: 70000,
yoj: 2016,
address: {
dno: 456,
street: 3,
locality: 'Database Lane',
city: 'San Diego'
}
}
]
db.emp.find({ename:"Rahul"},{"address.city":1,_id:0})
[ { address: { city: 'San Diego' } } ]
db.emp.find({desig:"developer"})
[
{
_id: ObjectId('6673fad96cc1a0e2a1b04e19'),
eid: 3,
ename: 'John',
dept: 'IT',
desig: 'developer',
salary: 65000,
yoj: 2016,
address: {
dno: 789,
street: 8,
locality: 'Tech Hub',
city: 'San Francisco'
}
},
{
_id: ObjectId('6673fad96cc1a0e2a1b04e21'),
eid: 11,
ename: 'Rahul',
dept: 'IT',
desig: 'developer',
salary: 75000,
yoj: 2016,
address: {
dno: 456,
street: 3,
locality: 'Database Lane',
city: 'San Diego'
}
}
]
Agenda: Create database, Create collection, insert data, find, sort, limit, $all, $in.
Create a book Database with the fields: (isbn, bname, author[], year, publisher, price)
Questions:
1. list all the documents.
db.books.find()
[
{
_id: ObjectId('6677e7965b657213134b7628'),
isbn: 'e40',
bname: 'let us C',
author: [ 'yeshanth', 'kanaka' ],
year: 2012,
publisher: 'pearson',
price: 100
},
{
_id: ObjectId('6677e7ce5b657213134b7629'),
isbn: 'e41',
bname: 'Advanced Java',
author: [ 'rudresh' ],
year: 2015,
publisher: 'mcgraw hill',
price: 150
},
{
_id: ObjectId('6677e7ce5b657213134b762a'),
isbn: 'e42',
bname: 'Python Programming',
author: [ 'herbet', 'sheldon' ],
year: 2018,
publisher: 'pearson',
price: 120
},
{
_id: ObjectId('6677e7ce5b657213134b762b'),
isbn: 'e43',
bname: 'let us see',
author: [ 'kuvempu' ],
year: 2011,
publisher: 'oxford',
price: 200
},
{
_id: ObjectId('6677e7ce5b657213134b762c'),
isbn: 'e44',
bname: 'Java for Beginners',
author: [ 'rama', 'krishna' ],
year: 2019,
publisher: 'pearson',
price: 90
}
]
b> db.books.find({publisher:"pearson"})
[
{
_id: ObjectId('6677e7965b657213134b7628'),
isbn: 'e40',
bname: 'let us C',
author: [ 'yeshanth', 'kanaka' ],
year: 2012,
publisher: 'pearson',
price: 100
},
{
_id: ObjectId('6677e7ce5b657213134b762a'),
isbn: 'e42',
bname: 'Python Programming',
author: [ 'herbet', 'sheldon' ],
year: 2018,
publisher: 'pearson',
price: 120
},
{
_id: ObjectId('6677e7ce5b657213134b762c'),
isbn: 'e44',
bname: 'Java for Beginners',
author: [ 'rama', 'krishna' ],
year: 2019,
publisher: 'pearson',
price: 90
}
]
db.books.find({bname:"Java"},{publisher:1})
[ { _id: ObjectId('6677e7ce5b657213134b762c'), publisher:
'pearson' } ]
bookdb>
db.books.find({bname:"let us C"},{author:1,publisher:1,year:1})
[
{
_id: ObjectId('6677e7965b657213134b7628'),
author: [ 'yeshanth', 'kanaka' ],
year: 2012,
publisher: 'pearson'
}
]
db.books.find({bname:"let us C"},{price:1,_id:0})
[ { price: 100 } ]
bookdb>
db.books.find().sort({bname:1})
[
{
_id: ObjectId('6677e7ce5b657213134b7629'),
isbn: 'e41',
bname: 'Advanced Java',
author: [ 'rudresh' ],
year: 2015,
publisher: 'mcgraw hill',
price: 150
},
{
_id: ObjectId('6677e7ce5b657213134b762c'),
isbn: 'e44',
bname: 'Java',
author: [ 'rama', 'krishna' ],
year: 2019,
publisher: 'pearson',
price: 90
},
{
_id: ObjectId('6677e7965b657213134b7628'),
isbn: 'e40',
bname: 'Let us C',
author: [ 'yeshanth', 'kanaka' ],
year: 2012,
publisher: 'pearson',
price: 100
},
{
_id: ObjectId('6677e7ce5b657213134b762b'),
isbn: 'e43',
bname: 'Let us see',
author: [ 'kuvempu' ],
year: 2011,
publisher: 'oxford',
price: 200
},
{
_id: ObjectId('6677e7ce5b657213134b762a'),
isbn: 'e42',
bname: 'Python Programming',
author: [ 'herbet', 'sheldon' ],
year: 2018,
publisher: 'pearson',
price: 120
}
]
db.books.find().sort({price:-1}).skip(2)
[
{
_id: ObjectId('6677e7ce5b657213134b762a'),
isbn: 'e42',
bname: 'Python Programming',
author: [ 'herbet', 'sheldon' ],
year: 2018,
publisher: 'pearson',
price: 120
},
{
_id: ObjectId('6677e7965b657213134b7628'),
isbn: 'e40',
bname: 'Let us C',
author: [ 'yeshanth', 'kanaka' ],
year: 2012,
publisher: 'pearson',
price: 100
},
{
_id: ObjectId('6677e7ce5b657213134b762c'),
isbn: 'e44',
bname: 'Java',
author: [ 'rama', 'krishna' ],
year: 2019,
publisher: 'pearson',
price: 90
}
]
db.books.find({author:{$all:["herbet","kuvempu"]}})
11. Display all the books either written by herbet and kuvempu
db.books.find({author:{$in:["herbet","kuvempu"]}})
[
{
_id: ObjectId('6677e7ce5b657213134b762a'),
isbn: 'e42',
bname: 'Python Programming',
author: [ 'herbet', 'sheldon' ],
year: 2018,
publisher: 'pearson',
price: 120
},
{
_id: ObjectId('6677e7ce5b657213134b762b'),
isbn: 'e43',
bname: 'Let us see',
author: [ 'kuvempu' ],
year: 2011,
publisher: 'oxford',
price: 200
}
]
Agenda: Create database, Create collection, insert data, find, find one, update, upsert, multi.
Create a Food Database with the fields: (food id, food cat, food name, chef name[ ], price,
ingredients[], hotel name, hotel address { no, street, locality, city})
Questions:
1. list the price of pizza with ingredients.
db.foods.find({foodname:"pizza"},{foodname:1,ingredients:1})
[
{
_id: ObjectId('667911d22461cc8823b73686'),
foodname: 'pizza',
ingredients: [ 'tomato', 'cheese', 'pepperoni' ]
}
]
\
db.foods.find({price:{$gt:500,$lt:800}})
[
{
_id: ObjectId('667911d22461cc8823b73686'),
foodid: 2,
foodcat: 'fast food',
foodname: 'pizza',
chefname: [ 'raj', 'anita' ],
price: 700,
ingredients: [ 'tomato', 'cheese', 'pepperoni' ],
hotelname: 'pizzahut',
address: {
no: 15,
street: 'brigade road',
locality: 'shanthinagar',
city: 'bangalore'
}
}
]
db.foods.updateOne({foodname:"burger"},{$addToSet:{ingredients:"meat"}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
fooddb> db.foods.find({foodname:"burger"})
[
{
_id: ObjectId('667911d22461cc8823b73685'),
foodid: 1,
foodcat: 'fast food',
foodname: 'burger',
chefname: [ 'naveen', 'rakesh' ],
price: 500,
ingredients: [ 'cheese', 'corn', 'meat' ],
hotelname: 'mcburger',
address: {
no: 31,
street: 'belroad',
locality: 'yelahanka',
city: 'bangalore'
}
}
]
db.foods.updateOne({foodname:"burger"},{$set:{foodType:"junkFood"}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
fooddb> db.foods.find({foodname:"burger"})
[
{
_id: ObjectId('667911d22461cc8823b73685'),
foodid: 1,
foodcat: 'fast food',
foodname: 'burger',
chefname: [ 'naveen', 'rakesh' ],
price: 500,
ingredients: [ 'cheese', 'corn' ],
hotelname: 'mcburger',
address: {
no: 31,
street: 'belroad',
locality: 'yelahanka',
city: 'bangalore'
},
foodType: 'junkFood'
}
]
11. Add or insert a new food item with the food Id “f08 “ using upsert as True.
db.foods.updateOne({foodid:"f08"},{$set:{ foodcat: "snack", foodname:
"sandwich", chefname: ["arjun"], price: 50, ingredients: ["bread", "vegetables"],
hotelname: "quick bite", address: { no: 12, street: "koramangala", locality:
"koramangala", city: "bangalore" }}},{upsert:true})
{
acknowledged: true,
insertedId: ObjectId('667931d910bd9d38558fd631'),
matchedCount: 0,
modifiedCount: 0,
upsertedCount: 1
}
fooddb> db.foods.find({foodname:"sandwich"})
[
{
_id: ObjectId('667931d910bd9d38558fd631'),
foodid: 'f08',
address: {
no: 12,
street: 'koramangala',
locality: 'koramangala',
city: 'bangalore'
},
chefname: [ 'arjun' ],
foodcat: 'snack',
foodname: 'sandwich',
hotelname: 'quick bite',
ingredients: [ 'bread', 'vegetables' ],
price: 50
}
]
12. Increment the price of all food item in food cat: fastfood by 120.
db.foods.find({foodcat:"fast food"})
[
{
_id: ObjectId('667911d22461cc8823b73685'),
foodid: 1,
foodcat: 'fast food',
foodname: 'burger',
chefname: [ 'naveen', 'rakesh' ],
price: 320,
ingredients: [ 'cheese', 'corn' ],
hotelname: 'mcburger',
address: {
no: 31,
street: 'belroad',
locality: 'yelahanka',
city: 'bangalore'
},
foodType: 'junkFood'
},
{
_id: ObjectId('667911d22461cc8823b73686'),
foodid: 2,
foodcat: 'fast food',
foodname: 'pizza',
chefname: [ 'raj', 'anita', 'shankar' ],
price: 820,
ingredients: [ 'tomato', 'cheese', 'pepperoni' ],
hotelname: 'pizzahut',
address: {
no: 15,
street: 'brigade road',
locality: 'shanthinagar',
city: 'bangalore'
}
}
]