Mongo Dbcoursera
Mongo Dbcoursera
Objectives
After completing this lab, you will be able to:
You will notice MongoDB is listed there but inactive. Which means the database is not available for use.
Once you select MongoDB, you will see more details and a place to start it.
about:blank 1/7
4/30/24, 2:40 PM about:blank
Clicking Start will run a background process to configure and start your MongoDB server.
Shortly after that, your server is ready for use. This deployment has access control enabled, and MongoDB enforces authentication. So, take note of the password, as
you will need it to log in as the root user.
about:blank 2/7
4/30/24, 2:40 PM about:blank
You can now open the terminal and enter details yourself.
This action will open a new terminal at the end of the screen, as in the following image.
about:blank 3/7
4/30/24, 2:40 PM about:blank
Run the following command on the newly opened terminal. (Copy the code by selecting copy on the right of the code block and then paste it wherever you wish.)
1. 1
Copied! Executed!
The command contains the username and password to connect to the MongoDB server (the text after the -p option is the password). Your output will be different
from the one shown here. Copy the command given to you, or click MongoDB CLI. You will need this command in the next step.
about:blank 4/7
4/30/24, 2:40 PM about:blank
1. db.version()
Copied!
1. show dbs
Copied!
This command will print a list of the databases present on the server, including default and user-defined.
1. use training
Copied!
This command will switch the context to the database named training. If the database training doesn't exist, MongoDB will create it for you when you insert data.
1. db.createCollection("mycollection")
Copied!
This command will create a collection name mycollection inside the training database.
about:blank 5/7
4/30/24, 2:40 PM about:blank
1. 1
1. show collections
Copied!
This command will print the list of collections in your current database.
1. db.mycollection.insert({"color":"white","example":"milk"})
Copied!
The above command inserts the json document {"color":"white","example":"milk"} into the collection.
1. db.mycollection.insert({"color":"blue","example":"sky"})
Copied!
The previous command inserts the JSON document {"color":"blue","example":"sky"} into the collection.
1. db.mycollection.countDocuments()
Copied!
1. db.mycollection.find()
Copied!
Notice that MongoDB automatically adds an _id field to every document to identify the document.
1. exit
Copied!
Practice exercises
1. Problem:
2. Problem:
List databases.
3. Problem:
about:blank 6/7
4/30/24, 2:40 PM about:blank
Create a database named mydatabase.
4. Problem:
5. Problem:
List collections
6. Problem:
Insert details of five landmarks, including name, city, and country. Example: Eiffel Tower, Paris, France.
7. Problem:
8. Problem:
9. Problem:
Summary
In this lab, you have gained an understanding of basic commands to interact with MongoDB by performing Insert and Read operations.
Author(s)
Muhammad Yahya
about:blank 7/7