0% found this document useful (0 votes)
22 views47 pages

MongoDB Exercises: Student & Employee Database

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views47 pages

MongoDB Exercises: Student & Employee Database

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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
}
> [Link](doc1)

Note: insert 10 documents.

Questions:
[Link] all the documents

studb9> [Link]()
[
{
_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
}
]
[Link] all the students in BCA

[Link]({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
}
]

[Link] all the students in ascending order


[Link]().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
}
]

[Link] first 5 students

[Link]().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
}
]
[Link] students 5,6,7
[Link]().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
}
]

[Link] the degree of student "Rahul"

[Link]({sname:"Rahul"},{degree:1,_id:0})
[ { degree: 'BCA' } ]

[Link] students details of 5,6,7 in descending order of percentage


[Link]().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
}
]

[Link] the number of students in BCA

[Link]({degree:"BCA"})
2

[Link] all the degrees without _id

[Link]({},{degree:1,_id:0})
[
{ degree: 'BCA' },
{ degree: 'BSc' },
{ degree: 'BCom' },
{ degree: 'BA' },
{ degree: 'BBA' },
{ degree: 'BCA' },
{ degree: 'BSc' },
{ degree: 'BCom' },
{ degree: 'BA' },
{ degree: 'BBA' }

[Link] all the distinct degrees


[Link]("degree")
[ 'BA', 'BBA', 'BCA', 'BCom', 'BSc' ]

[Link] all the BCA students with CGPA greater than 6, but less than 7.5
[Link]({degree:"BCA",CGPA:{$gt:6,$lt:7.5}})
[
{
_id: ObjectId('6672e34374ce5d4edb2a7c4d'),
srn: 110,
sname: 'Rahul',
degree: 'BCA',
sem: 6,
CGPA: 6.5
}
]

[Link] all the students in BCA and in 6th Sem


[Link]({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"
}
}
> [Link](doc1)
WriteResult({ "nInserted" : 1 })

Note: insert 10 documents.

Questions:
[Link] all the employees with salary in range (50000, 75000)
[Link]({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'
}
}
]

[Link] all the employees with desig developer

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

[Link] the Salary of “Rahul”


[Link]({ename:"Rahul"},{salary:1,_id:0})
[ { salary: 70000 } ]

[Link] the city of employee “Rahul”

[Link]({ename:"Rahul"},{"[Link]":1,_id:0})
[ { address: { city: 'San Diego' } } ]

5. Update the salary of developers by 5000 increment

[Link]({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”


[Link]({ename:"Rahul"},{$set:{age:26}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
employee> [Link]({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”
[Link]({ename:"Rahul"},{$unset:{yoj:1}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
employee> [Link]({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”


[Link]({ename:"Rahul"},{$set:{project2:[],project3:
[]}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
employee> [Link]({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”


[Link]({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”
[Link]({ename:"Rahul"},{$set:{contact:{phone:
[6457839295],email:["rahul@[Link]"]}}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
[Link]({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: [
'rahul@[Link]' ] }
}
]

12. Add two phone numbers to “Rahul”


[Link]({ename:"Rahul"},{$push:
{"[Link]":9955465935}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
employee> [Link]({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: [
'rahul@[Link]' ] }
}
]
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
}`
> [Link](doc1)

Note: insert 5 documents.

Questions:
1. list all the documents.

[Link]()
[
{
_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.


[Link]({},{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
}
]

[Link] all the books authored by rudresh


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

[Link] all the books published by pearson

b> [Link]({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
}
]

[Link] the publisher of book java

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

[Link] the author,publisher and year of the book let us see.

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

[Link] the price of “let us C” except _id

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

[Link] and display all books in ascending order of book names

[Link]().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
}
]

[Link] and display only 3 books in descending order of price.

[Link]().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
}
]

[Link] all the books written by herbet and kuvempu

[Link]({author:{$all:["herbet","kuvempu"]}})

11. Display all the books either written by herbet and kuvempu
[Link]({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
}
]

[Link] all the books where rama is the first author


[Link]({"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.
[Link]({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)

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

[Link] the item prepared by x and y

[Link]({ 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


[Link]({ 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
[Link]({foodname:"pizza"},{$addToSet:{chefname:"shankar"}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
fooddb> [Link]({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

[Link]({foodname:"burger"},{$addToSet:{ingredients:"meat"}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
fooddb> [Link]({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


[Link]({foodname:"burger"},{$pop:{ingredients:1}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
fooddb> [Link]({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'
}
}
]

[Link] All the ingredients from the food biryani


[Link]({foodname:"biryani"},{$unset:{ingredients:''}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
fooddb> [Link]({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.

[Link]({foodname:"burger"},{$set:{foodType:"junkFood"}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
fooddb> [Link]({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


[Link]({foodname:"burger"},{$set:{price:200}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
fooddb> [Link]({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.
[Link]({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> [Link]({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.

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

Common questions

Powered by AI

To identify food items prepared by both chefs 'Raj' and 'Anita', use the query: `db.foods.find({ chefname: { $all: ["Raj", "Anita"] } })`, which returns items prepared by both specified chefs .

To add a new contact with an email and multiple phone numbers for an employee 'Rahul', execute: `db.emp.updateOne({ename:"Rahul"},{$set:{contact:{phone: [6457839295, 9955465935],email:["rahul@gmail.com"]}}})`. This adds a 'contact' embedded object containing phone numbers and email address to Rahul's record .

To remove the 'yoj' field from Rahul's employee document, use: `db.emp.updateOne({ename:"Rahul"},{$unset:{yoj:1}})`. This command removes the 'yoj' field from the specified record .

To list all book names with their authors while excluding year and price, execute: `db.books.find({}, {year:0, price:0})`. This returns documents containing all details except the specified fields, effectively removing 'year' and 'price' from the results .

You can retrieve the salary information of an employee named 'Rahul' using the query: `db.emp.find({ename:"Rahul"},{salary:1,_id:0})`, which specifically returns only the salary field of Rahul's record .

To list all distinct degrees present in student records, use the command: `db.stud09.distinct("degree")`. This returns ['BA', 'BBA', 'BCA', 'BCom', 'BSc'].

To add an 'age' field for the student 'Rahul', you would execute the command: `db.emp.updateOne({ename:"Rahul"},{$set:{age:26}})` which adds the field 'age' with the value 26 to Rahul's record .

To increment the price of all fast food items by 120, use the command: `db.foods.updateMany({foodcat:"fast food"}, {$inc:{price:120}})`. This operation increases the price field by 120 for all documents where the food category is 'fast food' .

To extract all records of students enrolled in the 'BCA' degree from the student database, execute the query: `db.stud09.find({degree:"BCA"})` which returns the documents for students Rahul and Pooja with their respective information .

To display students sorted by their CGPA in descending order, execute: `db.stud09.find().sort({CGPA:-1})`. This outputs the student documents sorted by CGPA from highest to lowest .

You might also like