MongoDB Class Exercise
Exercise 1 - Find the version of the server
On the mongo client run the below command.
db.version()
This will show the version of the mongodb server.
Exercise 2 - List databases
On the mongo client run the below command.
show dbs
This will print a list of the databases present on the server.
Exercise 3 - Create database
On the mongo client run the below command.
use training
This will create a new database named training. If a database named training already
exists, it will start using it.
Exercise 4 - Create collecton
On the mongo client run the below command.
db.createCollection("mycollection")
This will create a collection name mycollection inside the training database.
Exercise 5 - List collections
On the mongo client run the below command.
show collections
This will print the list of collections in your current database.
Exercise 6 - Insert documents into a
collection
On the mongo client run the below command.
insert() is deprecated
db.mycollection.insert({"color":"white","example":"milk"})
db.mycollection.insertOne({"color":"brown","example":"sugar"})
The above command inserts the json document {"color":"brown","example":"sugar"} into
the collection.
Let us insert one more document.
db.mycollection.insertOne({"color":"blue","example":"berry"})
The above command inserts the json document {"color":"blue","example":"berry"} into
the collection.
Insert 3 more documents of your choice.
Exercise 7 - Count the number of
documents in a collection
On the mongo client run the below command.
count() is deprecated
db.mycollection.count()
db.mycollection. countDocuments()
This command gives you the number of documents in the collection.
Exercise 8 - List all documents in a
collection
On the mongo client run the below command.
db.mycollection.find()
This command lists all the documents in the collection mycollection
Notice that mongodb automatically adds an '_id' field to every document in order to
uniquely identify the document.
Exercise 9 - Disconnect from mongodb
server
On the mongo client run the below command.
exit
Practice exercises
1. Problem:
Connect to mongodb server.
2. Problem:
List databases.
3. Problem:
Create a database named mydatabase.
4. Problem:
Create a collection named landmarks in the database mydatabase.
5. Problem:
List collections
6. Problem:
Insert details of five landmarks including name, city, and country. Example: Eiffel Tower,
Paris, France.
db.landmarks.insertOne({"name":"Statue of Liberty","city":"New York","country":"USA"})
db.landmarks.insertOne({"name":"Big Ben","city":"London","country":"UK"})
db.landmarks.insertOne({"name":"Taj Mahal","city":"Agra","country":"India"})
db.landmarks.insertOne({"name":"Pyramids","country":"Egypt"})
db.landmarks.insertOne({"name":"Great Wall of China","country":"China"})
7. Problem:
Count the number of documents you have inserted.
8. Problem:
List the documents.
9. Problem:
Disconnect from the server.
MongoDB CRUD
Estimated time needed: 30 minutes
Objectives
After completing this lab, you will be able to:
• Create documents in MongoDB with the insert method
• Read documents by listing them, counting them and matching them to a query
• Update and delete documents in MongoDB based on specific criteria
Exercise 1 - Getting the environment ready
1. Problem:
Start the mongodb server.
2. Problem:
Connect to the mongodb server.
3. Problem:
Select the training database.
4. Problem:
Create a collection named languages.
Exercise 2 - Insert documents
Let us insert five documents into the collection languages
On the mongo shell run the below commands.
db.languages.insert({"name":"java","type":"object oriented"})
db.languages.insert({"name":"python","type":"general purpose"})
db.languages.insert({"name":"scala","type":"functional"})
db.languages.insert({"name":"c","type":"procedural"})
db.languages.insert({"name":"c++","type":"object oriented"})
Exercise 3 - Read documents
Let us try out different ways of querying documents.
Find the count of documents.
db.languages.countDocuments()
List the first document in the collection.
db.languages.findOne()
List all documents in the collection.
db.languages.find()
List first 3 documents in the collection.
db.languages.find().limit(3)
Query for "python" language.
db.languages.find({"name":"python"})
Query for "object oriented" languages.
db.languages.find({"type":"object oriented"})
List only specific fields.
Using a projection document you can specify what fields we wish to see or skip in the
output.
This command lists all the documents with only name field in the output.
db.languages.find({},{"name":1})
This command lists all the documents without the name field in the output.
db.languages.find({},{"name":0})
This command lists all the "object oriented" languages with only "name" field in the
output.
db.languages.find({"type":"object oriented"},{"name":1})
Exercise 4 - Update documents
Update documents based on a criteria.
Add a field to all the documents.
The 'updateMany' command is used to update documents in a mongodb collection, and
it has the following generic syntax.
db.collection.updateMany({what documents to find},{$set:{what fields to set}})
Here we are adding a field description with value programming language to all the
documents.
db.languages.updateMany({},{$set:{"description":"programming language"}})
Set the creator for python language.
db.languages.updateMany({"name":"python"},{$set:{"creator":"Guido van Rossum"}})
Set a field named compiled with a value true for all the object oriented languages.
db.languages.updateMany({"type":"object oriented"},{$set:{"compiled":true}})
Exercise 5 - Delete documents
Delete documents based on a criteria.
Delete the scala language document.
db.languages.remove({"name":"scala"})
Delete the object oriented languages.
db.languages.remove({"type":"object oriented"})
Delete all the documents in a collection.
db.languages.remove({})
Practice exercises
Run the below code on mongo console. It will insert 5 documents, which will serve as
sample data for the next steps.
use training
db.languages.insert({"name":"java","type":"object oriented"})
db.languages.insert({"name":"python","type":"general purpose"})
db.languages.insert({"name":"scala","type":"functional"})
db.languages.insert({"name":"c","type":"procedural"})
db.languages.insert({"name":"c++","type":"object oriented"})
1. Problem:
Insert an entry for 'Haskell' programming language which is of type 'functional .
Click here for Hint
Click here for Solution
db.languages.insert({"name":"Haskell","type":"functional"})
2. Problem:
Query for all functional languages.
Click here for Hint
Click here for Solution
db.languages.find({"type":"functional"})
3. Problem:
Add 'Bjarne Stroustrup' as creator for c++.
Click here for Hint
Click here for Solution
db.languages.updateMany({"name":"c++"},{$set:{"creator":" Bjarne Stroustrup "}})
4. Problem:
Delete all functional programming languages.
Click here for Hint
Click here for Solution
db.languages.remove({"type":"functional"})
5. Problem:
Disconnect from the mongodb server.
Click here for Hint
Click here for Solution
exit