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
2.Display all the students in BCA
3.Display all the students in ascending order
4.Display first 5 students
5.display students 5,6,7
6.list the degree of student "Rahul"
7.Display students details of 5,6,7 in descending order of percentage
8.Display the number of students in BCA
9.Display all the degrees without _id
10.Display all the distinct degrees
11.Display all the BCA students with CGPA greater than 6, but less than 7.5
12.Display all the students in BCA and in 6th Sem
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)
2.Display all the employees with desig developer
3.Display the Salary of “Rahul”
4.Display the city of employee “Rahul”
5. Update the salary of developers by 5000 increment
6. Add field age to employee “Rahul”
7. Remove YOJ from “Rahul”
8. Add an array field project to “Rahul”
9. Add p2 and p3 project to “Rahul”
10. Remove p3 from “Rahul”
11. Add a new embedded object “contacts” with “email” and “phone” as array objects to “Rahul”
12. Add two phone numbers to “Rahul”
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.
2. list all the book name except year and price.
3.display all the books authored by rudresh
4.list all the books published by pearson
5.list the publisher of book java
6.list the author,publisher and year of the book let us see.
7.Display the price of “let us see” except _id
8.sort and display all books in ascending order of book names
9.sort and display only 3 books in descending order of price.
10.Display all the books written by herbet and kuvempu
11. Display all the books either written by herbet and kuvempu
12.display all the books where rama is the first author
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.
2. display the item in the price range(500,800)
3.display the item prepared by x and y
4. Display the item prepared by x or y
5. Add one chef to the food pizza
6. Add ingredients to the food Burger
7. Delete last ingredient added to the food burger
8.Delete All the ingredients from the food biryani
9. Add food type to the food Burger.
10. Modify the burger price by 200
11. Add or insert a new food item with the food Id “f08 “ using upsert as True.
12. Increment the price of all food item in food cat: fastfood by 120.