You are on page 1of 4

MongoDB Hands-on Exercises

By Dr. Ruhaila Maskat

Installing Mongo
Go to https://www.youtube.com/playlist?list=PLydSrzyx1wVOSqIUnmzxHY9dUUa2qJoXR

View video on MongoDB installation for Windows and follow step by step.

DATA MANIPULATION COMMANDS

1. List existing databases.


show dbs

2. Create a database called dominos, if not yet existed, or start using the database.
use dominos

3. Show current working database.


db

4. If database already exist and to start using it.


use [database name]

e.g.
use kfc
use mydb

5. Create a new collection of customers.


db.createCollection(“customers”)

6. List all collections in the dominos database.


show collections

7. If you want to delete/drop the customers collection.


db.customers.drop()
8. Create a new document in collection customers.
db.customers.insertOne({
first_name:”Ali”,
last_name:”Manaf”})

9. Insert the following customers into the customers collection.


db.customers.insertMany([
{first_name:"Ali",last_name:"Imran"},
{first_name:"Ali",last_name:"Samad"},
{first_name:"Siti",last_name:"Amri"},
{first_name:"Fatimah",last_name:"Ismail"},
{first_name:"Khadijah",last_name:"Nasir"}
])

View all documents in customers collection.


db.customers.find()

UPDATE COMMANDS

10. Update only the first document with the name Ali to insert gender.
db.customers.updateOne({first_name:"Ali"},{$set:{gender:"male"}})

View all documents in customers collection.


db.customers.find()

11. Set Ali’s age to 45


db.customers.updateOne({first_name:”Ali”}, {$set: {age:45}})

12. Ooops, Ali’s age is actually 48, use increment to add 3 to his age.
db.customers.updateOne({first_name:”Ali”}, {$inc: {age:3}})

13. Remove a name/value. In this case, age. 1 for true 0 for false.
db.customers.updateOne({first_name:”Ali”}, {$unset:{age:1}})
14. Rename a name of a name/value
db.customers.updateOne({first_name:”Ali”},{$rename:{“gender”:”sex”}})

15. Update all Ali to have age = 40

db.customers.updateMany({first_name:”Ali”}, {$set:{age:40}})

View all documents in customers collection.


db.customers.find()

16. Set all customers to have the age of 40


db.customers.updateMany({},{$set:{age:40}})

17. Update different customers with different ages from 20 to 40 (to be done on your own)

18. Set the gender for each customer accordingly (to be done on your own)

DELETE COMMANDS

19. Remove the first document with the first name Ali.
db.customers.deleteOne({first_name:"Ali"})

20. Remove all documents with the first name Ali.


db.customers.deleteMany({first_name:”Ali”
});

SEARCH COMMANDS

21. List all documents in the collection customers.


db.customers.find();

22. Find documents where last name is named Imran or first name is Siti using the
OR operator.
db.customers.find({$or:[{last_name:"Imran"},{first_name:"Siti"}]})

23. Find documents where last name is named Imran or first name is Siti using the
OR operator.
db.customers.find({$or:[{last_name:"Imran"},{first_name:"Siti"}]})
24. Find documents where age is greater than 30

db.customers.find({age:{$gt:30}});

25. Count the number of documents in collection customers


db.customers.find().count();

26. Count the number of documents in collection customers that has male as gender.
db.customers.find({gender:”male”}).count();

27. Select the full name of customers where age is greater than 30
db.customers.find({age:{$gt:30}},{first_name:1, last_name:1})

You might also like