0% found this document useful (0 votes)
2 views

Basic Exercise MongoDB

The document contains exercises for creating and manipulating MongoDB databases for students and employees. It includes tasks such as creating databases, inserting documents, querying data, and using various MongoDB commands to filter and sort results. The exercises cover operations like finding specific records, counting documents, and displaying distinct values.

Uploaded by

wub0fuhubis3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Basic Exercise MongoDB

The document contains exercises for creating and manipulating MongoDB databases for students and employees. It includes tasks such as creating databases, inserting documents, querying data, and using various MongoDB commands to filter and sort results. The exercises cover operations like finding specific records, counting documents, and displaying distinct values.

Uploaded by

wub0fuhubis3
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

1 MongoDB Excersices

Exercise 1 - Student Database

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)

> use studb9


switched to db studb9

> 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)

Note: insert 10 documents.

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
}
]

3.Display all the students in ascending order


db.stud09.find().sort({sname:1})
[
{
_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
},
{
_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
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c55'),
srn: 118,
sname: 'Ravi',
degree: 'BA',
sem: 6,
CGPA: 7.6
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c50'),
srn: 113,
sname: 'Sneha',
degree: 'BA',
sem: 6,
CGPA: 8
},
{
_id: ObjectId('6672e34374ce5d4edb2a7c4f'),
srn: 112,
sname: 'Vikas',
degree: 'BCom',
sem: 6,
CGPA: 7.5
}
]

4.Display first 5 students

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
}
]

6.list the degree of student "Rahul"

db.stud09.find({sname:"Rahul"},{degree:1,_id:0})
[ { degree: 'BCA' } ]

7.Display students details of 5,6,7 in descending order of percentage


db.stud09.find().sort({CGPA:-1}).skip(4).limit(3)
[
{
_id: ObjectId('6672e34374ce5d4edb2a7c4d'),
srn: 110,
sname: 'Rahul',
degree: 'BCA',
sem: 6,
CGPA: 7.9
},
{
_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
}
]

8.Display the number of students in BCA

db.stud09.countDocuments({degree:"BCA"})
2

9.Display all the degrees without _id

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' }

10.Display all the distinct degrees


db.stud09.distinct("degree")
[ 'BA', 'BBA', 'BCA', 'BCom', 'BSc' ]

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
}
]

12.Display all the students in BCA and in 6th Sem


db.stud09.find({degree:"BCA",sem:6,CGPA:{$gt:6,$lt:7.5}})
[
{
_id: ObjectId('6672e34374ce5d4edb2a7c4d'),
srn: 110,
sname: 'Rahul',
degree: 'BCA',
sem: 6,
CGPA: 6.5
}
]

Exercise 2 - Employee Database


Agenda: Update modifiers ($set, $unset, $inc, $push, $pushAll, $pull, $pullAll, $addToSet)

Create an employee database with the fields: {eid, ename, dept, desig, salary, yoj, address{dno,
street, locality, city}}

> use empdb9


switched to db empdb9

> doc1 = {eid:001, ename:"Rahul", dept:"production", desig:"developer", salary:30000, yoj:2015,


address:{dno:397, street:2, locality:"rmnagar", city:"bangalore"} }
{
"eid" : 1,
"ename" : "Rahul",
"dept" : "production",
"desig" : "developer",
"salary" : 30000,
"yoj" : 2015,
"address" : {
"dno" : 397,
"street" : 2,
"locality" : "rmnagar",
"city" : "bangalore"
}
}
> db.emp09.insert(doc1)
WriteResult({ "nInserted" : 1 })

Note: insert 10 documents.

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'
}
}
]

2.Display all the employees with desig developer

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'
}
}
]

3.Display the Salary of “Rahul”


db.emp.find({ename:"Rahul"},{salary:1,_id:0})
[ { salary: 70000 } ]

4.Display the city of employee “Rahul”

db.emp.find({ename:"Rahul"},{"address.city":1,_id:0})
[ { address: { city: 'San Diego' } } ]

5. Update the salary of developers by 5000 increment

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'
}
}
]

6. Add field age to employee “Rahul”


db.emp.updateOne({ename:"Rahul"},{$set:{age:26}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
employee> db.emp.find({ename:"Rahul"})
[
{
_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'
},
age: 26
}
]
7. Remove YOJ from “Rahul”
db.emp.updateOne({ename:"Rahul"},{$unset:{yoj:1}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
employee> db.emp.find({ename:"Rahul"})
[
{
_id: ObjectId('6673fad96cc1a0e2a1b04e21'),
eid: 11,
ename: 'Rahul',
dept: 'IT',
desig: 'developer',
salary: 75000,
address: {
dno: 456,
street: 3,
locality: 'Database Lane',
city: 'San Diego'
},
age: 26
}
]
8. Add an array field project to “Rahul”

9. Add p2 and p3 project to “Rahul”


db.emp.updateOne({ename:"Rahul"},{$set:{project2:[],project3:
[]}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
employee> db.emp.find({ename:"Rahul"})
[
{
_id: ObjectId('6673fad96cc1a0e2a1b04e21'),
eid: 11,
ename: 'Rahul',
dept: 'IT',
desig: 'developer',
salary: 75000,
address: {
dno: 456,
street: 3,
locality: 'Database Lane',
city: 'San Diego'
},
age: 26,
project1: [],
project2: [],
project3: []
}
]

10. Remove p3 from “Rahul”


db.emp.find({ename:"Rahul"})
[
{
_id: ObjectId('6673fad96cc1a0e2a1b04e21'),
eid: 11,
ename: 'Rahul',
dept: 'IT',
desig: 'developer',
salary: 75000,
address: {
dno: 456,
street: 3,
locality: 'Database Lane',
city: 'San Diego'
},
age: 26,
project1: [],
project2: []
}
]
11. Add a new embedded object “contacts” with “email” and “phone” as array objects to “Rahul”
db.emp.updateOne({ename:"Rahul"},{$set:{contact:{phone:
[6457839295],email:["[email protected]"]}}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
db.emp.find({ename:"Rahul"})
[
{
_id: ObjectId('6673fad96cc1a0e2a1b04e21'),
eid: 11,
ename: 'Rahul',
dept: 'IT',
desig: 'developer',
salary: 75000,
address: {
dno: 456,
street: 3,
locality: 'Database Lane',
city: 'San Diego'
},
age: 26,
project1: [],
contact: { phone: [ 6457839295 ], email: [
'[email protected]' ] }
}
]

12. Add two phone numbers to “Rahul”


db.emp.updateOne({ename:"Rahul"},{$push:
{"contact.phone":9955465935}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
employee> db.emp.find({ename:"Rahul"})
[
{
_id: ObjectId('6673fad96cc1a0e2a1b04e21'),
eid: 11,
ename: 'Rahul',
dept: 'IT',
desig: 'developer',
salary: 75000,
address: {
dno: 456,
street: 3,
locality: 'Database Lane',
city: 'San Diego'
},
age: 26,
project1: [],
contact: { phone: [ 6457839295, 9955465935 ], email: [
'[email protected]' ] }
}
]
Exercise 3 - Book Database

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)

> use bookdb


switched to db bookdb

> doc1=({isbn:"e40", bname:"let us C", author:["yeshanth", "kanaka"], year:2012,


publisher:"pearson", price:100})
{
"isbn" : "e40",
"bname" : "let us C",
"author" : [
"yeshanth",
"kanaka"
],
"year" : 2012,
"publisher" : "pearson",
"price" : 100
}`
> db.book.insert(doc1)

Note: insert 5 documents.

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
}
]

2. list all the book name except year and price.


db.books.find({},{year:0})
[
{
_id: ObjectId('6677e7965b657213134b7628'),
isbn: 'e40',
bname: 'let us C',
author: [ 'yeshanth', 'kanaka' ],
publisher: 'pearson',
price: 100
},
{
_id: ObjectId('6677e7ce5b657213134b7629'),
isbn: 'e41',
bname: 'Advanced Java',
author: [ 'rudresh' ],
publisher: 'mcgraw hill',
price: 150
},
{
_id: ObjectId('6677e7ce5b657213134b762a'),
isbn: 'e42',
bname: 'Python Programming',
author: [ 'herbet', 'sheldon' ],
publisher: 'pearson',
price: 120
},
{
_id: ObjectId('6677e7ce5b657213134b762b'),
isbn: 'e43',
bname: 'let us see',
author: [ 'kuvempu' ],
publisher: 'oxford',
price: 200
},
{
_id: ObjectId('6677e7ce5b657213134b762c'),
isbn: 'e44',
bname: 'Java for Beginners',
author: [ 'rama', 'krishna' ],
publisher: 'pearson',
price: 90
}
]

3.display all the books authored by rudresh


db.books.find({author:"rudresh"})
[
{
_id: ObjectId('6677e7ce5b657213134b7629'),
isbn: 'e41',
bname: 'Advanced Java',
author: [ 'rudresh' ],
year: 2015,
publisher: 'mcgraw hill',
price: 150
}
]

4.list all the books published by pearson

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
}
]

5.list the publisher of book java

db.books.find({bname:"Java"},{publisher:1})
[ { _id: ObjectId('6677e7ce5b657213134b762c'), publisher:
'pearson' } ]
bookdb>

6.list the author,publisher and year of the book let us see.

db.books.find({bname:"let us C"},{author:1,publisher:1,year:1})
[
{
_id: ObjectId('6677e7965b657213134b7628'),
author: [ 'yeshanth', 'kanaka' ],
year: 2012,
publisher: 'pearson'
}
]

7.Display the price of “let us C” except _id

db.books.find({bname:"let us C"},{price:1,_id:0})
[ { price: 100 } ]
bookdb>

8.sort and display all books in ascending order of book names

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
}
]

9.sort and display only 3 books in descending order of price.

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
}
]

10.Display all the books written by herbet and kuvempu

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
}
]

12.display all the books where rama is the first author


db.books.find({"author.0":"rama"})
[
{
_id: ObjectId('6677e7ce5b657213134b762c'),
isbn: 'e44',
bname: 'Java',
author: [ 'rama', 'krishna' ],
year: 2019,
publisher: 'pearson',
price: 90
}
]
Exercise 4 - Food Database

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})

> use fooddb


switched to db fooddb

> doc1= {foodid:1, foodcat:"fastfood", foodname:"burger", chefname:["naveen","rakesh"],


price:500,ingredients:["chees","corn"], hotelname:"mcburger", address:{no:31, street:"belroad",
locality:"yelahanka", city:"bangalore"}}
{
"foodid" : 1,
"foodcat" : "fast food",
"foodname" : "burger",
"chefname" : [
"naveen",
"rakesh"
],
"price" : 500,
"ingredients" : [
"chees",
"corn"
],
"hotelname" : "mcburger",
"address" : {
"no" : 31,
"street" : "belroad",
"locality" : "yelahanka",
"city" : "bangalore"
}
}

Note: insert 5 documents.

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' ]
}
]
\

2. display the item in the price range(500,800)

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'
}
}
]

3.display the item prepared by x and y

db.foods.find({ chefname: { $all: ["raj", "anita"] } })


[
{
_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'
}
}
]

4. Display the item prepared by x or y


db.foods.find({ chefname: { $in: ["raj", "anita"] } })
[
{
_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'
}
}
]
5. Add one chef to the food pizza
db.foods.updateOne({foodname:"pizza"},{$addToSet:{chefname:"shankar"}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
fooddb> db.foods.find({foodname:"pizza"})
[
{
_id: ObjectId('667911d22461cc8823b73686'),
foodid: 2,
foodcat: 'fast food',
foodname: 'pizza',
chefname: [ 'raj', 'anita', 'shankar' ],
price: 700,
ingredients: [ 'tomato', 'cheese', 'pepperoni' ],
hotelname: 'pizzahut',
address: {
no: 15,
street: 'brigade road',
locality: 'shanthinagar',
city: 'bangalore'
}
}
]

6. Add ingredients to the food Burger

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'
}
}
]

7. Delete last ingredient added to the food burger


db.foods.updateOne({foodname:"burger"},{$pop:{ingredients:1}})
{
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'
}
}
]

8.Delete All the ingredients from the food biryani


db.foods.updateOne({foodname:"biryani"},{$unset:{ingredients:''}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
fooddb> db.foods.find({foodname:"biryani"})
[
{
_id: ObjectId('66792a9a2461cc8823b7368a'),
foodid: 4,
foodcat: 'main course',
foodname: 'biryani',
chefname: [ 'kumar', 'ali' ],
price: 300,
hotelname: 'biryani house',
address: {
no: 10,
street: 'commercial street',
locality: 'frazer town',
city: 'bangalore'
}
}
]

9. Add food type to the food Burger.

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'
}
]

10. Modify the burger price by 200


db.foods.updateOne({foodname:"burger"},{$set:{price:200}})
{
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: 200,
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'
}
}
]

You might also like