10/2/23, 3:09 PM about:blank
Getting Started with MongoDB
Estimated time needed: 30 minutes
Objectives
After completing this lab you will be able to:
Access the MongoDB server using the command-line interface
Describe the process of listing and creating collections, which contain documents, and databases, which contain one or more collections
Perform basic operations on a collection such as inserting, counting and listing documents
About Skills Network Cloud IDE
Skills Network Cloud IDE (based on Theia and Docker) provides an environment for hands on labs for course and project related labs. Theia is an open
source IDE (Integrated Development Environment), that can be run on desktop or on the cloud. to complete this lab, we will be using the Cloud IDE based
on Theia and MongoDB running in a Docker container.
Important Notice about this lab environment
Please be aware that sessions for this lab environment are not persisted. Every time you connect to this lab, a new environment is created for you. Any data
you may have saved in the earlier session would get lost. Plan to complete these labs in a single session, to avoid losing your data.
Exercise 1 - Start mongodb server
Open a new terminal, by clicking on the menu bar and selecting Terminal->New Terminal, as in the image below.
This will open a new terminal at the bottom of the screen as in the image below.
about:blank 1/6
10/2/23, 3:09 PM about:blank
Run the below command on the newly opened terminal. (You can copy the code by clicking on the little copy button on the bottom right of the codeblock
below and then paste it, wherever you wish.)
1. 1
1. start_mongo
Copied!
This will start the mongodb server. It will also give you the command to connect to your instance of mongodb, as in the image below.
The command will look similar to the one given below.
1. 1
1. mongo -u root -p NTc0My1yc2FubmFy --authenticationDatabase admin local
Copied!
The command contains the username and password to connect to mongodb server (the text after the -p option is the password). Your output would be
different from the one shown above. Copy the command given to you, and keep it handy. You will need it in the next step.
Exercise 2 - Connect to mongodb server
On the terminal, paste or type the command you copied in the previous step, as in the image below.
about:blank 2/6
10/2/23, 3:09 PM about:blank
You should now get connected to the mongodb database, and see an output as in the figure below.
Exercise 3 - Find the version of the server
On the mongo client run the below command.
1. 1
1. db.version()
Copied!
This will show the version of the mongodb server.
Exercise 4 - List databases
On the mongo client run the below command.
1. 1
1. show dbs
Copied!
This will print a list of the databases present on the server.
Exercise 5 - Create database
On the mongo client run the below command.
1. 1
1. use training
Copied!
This will create a new database named training. If a database named training already exists, it will start using it.
Exercise 6 - Create collecton
On the mongo client run the below command.
1. 1
1. db.createCollection("mycollection")
about:blank 3/6
10/2/23, 3:09 PM about:blank
Copied!
This will create a collection name mycollection inside the training database.
Exercise 7 - List collections
On the mongo client run the below command.
1. 1
1. show collections
Copied!
This will print the list of collections in your current database.
Exercise 8 - Insert documents into a collection
On the mongo client run the below command.
1. 1
1. db.mycollection.insert({"color":"white","example":"milk"})
Copied!
The above command inserts the json document {“color”:”white”,”example”:”milk”} into the collection.
Let us insert one more document.
1. 1
1. db.mycollection.insert({"color":"blue","example":"sky"})
Copied!
The above command inserts the json document {“color”:”blue”,”example”:”sky”} into the collection.
Insert 3 more documents of your choice.
Exercise 9 - Count the number of documents in a collection
On the mongo client run the below command.
1. 1
1. db.mycollection.count()
Copied!
This command gives you the number of documents in the collection.
Exercise 10 - List all documents in a collection
On the mongo client run the below command.
1. 1
1. db.mycollection.find()
Copied!
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 11 - Disconnect from mongodb server
On the mongo client run the below command.
1. 1
1. exit
about:blank 4/6
10/2/23, 3:09 PM about:blank
Copied!
Practice exercises
1. Problem:
Connect to mongodb server.
Click here for Hint
Click here for Solution
2. Problem:
List databases.
Click here for Hint
Click here for Solution
3. Problem:
Create a database named mydatabase.
Click here for Hint
Click here for Solution
4. Problem:
Create a collection named landmarks in the database mydatabase.
Click here for Hint
Click here for Solution
5. Problem:
List collections
Click here for Hint
Click here for Solution
6. Problem:
Insert details of five landmarks including name, city, and country. Example: Eiffel Tower, Paris, France.
Click here for Hint
Click here for Solution
7. Problem:
Count the number of documents you have inserted.
Click here for Hint
Click here for Solution
8. Problem:
List the documents.
Click here for Hint
Click here for Solution
9. Problem:
Disconnect from the server.
Click here for Hint
Click here for Solution
Authors
Ramesh Sannareddy
Other Contributors
Rav Ahuja
about:blank 5/6
10/2/23, 3:09 PM about:blank
Change Log
Date (YYYY-MM-DD) Version Changed By Change Description
2021-10-19 0.4 Kathy An Updated learning objectives
2021-04-19 0.3 Steve Ryan Review pass
2021-03-16 0.2 Ramesh Sannareddy Added Hints/Solutions to Practise exercises
2021-02-24 0.1 Ramesh Sannareddy Created initial version of the lab
Copyright (c) 2021 IBM Corporation. All rights reserved.
about:blank 6/6