Crud
Crud
Crud
MongoDB CRUD
Objectives
After completing this lab, you will be able to:
You will notice MongoDB listed there, but inactive. Which means the database is not available to use.
Once you click on it, you will see more details about it and a button to start it.
about:blank 1/7
4/30/24, 2:55 PM about:blank
Clicking on the start button 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 login as root user.
about:blank 2/7
4/30/24, 2:55 PM about:blank
This will open a new terminal at the bottom of the screen as in the image below.
about:blank 3/7
4/30/24, 2:55 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
Copied! Executed!
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.
Or you can simply click on MongoDB CLI which does that for you.
about:blank 4/7
4/30/24, 2:55 PM about:blank
2. To insert more than one document at the same time, you can use insertMany command; which accepts an array as the argument.
1. 1
2. 2
3. 3
4. 4
5. 5
1. db.languages.insertMany([
2. {"name":"scala","type":"functional"},
3. {"name":"c","type":"procedural"},
4. {"name":"c++","type":"object oriented"}
5. ])
Copied!
about:blank 5/7
4/30/24, 2:55 PM about:blank
1. 1
1. db.languages.findOne()
Copied!
7. Use projection to only project 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.
1. 1
1. db.languages.find({},{"name":1})
Copied!
8. This command lists all the documents without the name field in the output.
1. 1
1. db.languages.find({},{"name":0})
Copied!
9. This command lists all the object oriented languages with only name field in the output.
1. 1
1. db.languages.find({"type":"object oriented"},{"name":1})
Copied!
The updateMany command is used to update documents in a mongodb collection, and it has the following generic syntax.
1. 1
1. db.collection.updateMany(<filter>,<update>)
Copied!
Here we are adding a field description with value programming language to all documents.
1. 1
1. db.languages.updateMany({},{$set:{"description":"programming language"}})
Copied!
3. Set a field named compiled with a value true for all the object oriented languages.
1. 1
1. db.languages.updateMany({"type":"object oriented"},{$set:{"compiled":true}})
Copied!
about:blank 6/7
4/30/24, 2:55 PM about:blank
1. 1
1. db.languages.deleteOne({"name":"scala"})
Copied!
Practice exercises
Run the below code on mongo console. It will insert 5 documents, which will serve as sample data for the next steps.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
1. use training
2. db.languages.insertMany([
3. {"name":"java","type":"object oriented"},
4. {"name":"python","type":"general purpose"},
5. {"name":"scala","type":"functional"},
6. {"name":"c","type":"procedural"},
7. {"name":"c++","type":"object oriented"}
8. ])
Copied!
1. Problem:
2. Problem:
3. Problem:
4. Problem:
5. Problem:
Summary
In this lab, you have gained the understanding of CRUD operations in MongoDB.
Author(s)
Muhammad Yahya
about:blank 7/7