In this documentation, I am gonna teach you the whole MongoDB CRUD operations in mongo shell from very scratch to the very end.
So, before using Mongo DB we must have installed that into our local system.
Installation
To install Mongo DB go to this download link.
Now choose your platform and simply download and install it.
Now the most important step comes into the picture i.e adding the path of the bin folder of the Mongo DB file to your environment variables. It will take too much time to explain to me, how to do that so in order to do that I am pasting a Youtube Link which will tell you how can you do that.
Naming conventions in MongoDB
So, now you have installed MongoDB on your PC, it's time to get some basic naming conventions that we will use while creating our Database.
I know an image is not enough to understand this. So, let's deep dive into it. In MongoDB, data are stored in the form of BSON format. Now, many of you are wondering what is BSON? BSON is the same as JSON format, and it looks like the same as JSON on the upper layer. But on the root layer, it deals in binary data format, and hence BSON stands for Binary JSON format. Now, why binary? The answer is very simple i.e working in binary data format will make it faster and hence the user experience will be enhanced.
A BSON data is look something like this only.
{
"name" : "Avinash Gupta",
"email" : "gavinash6290@gmail.com",
"degree" : "B.Tech"
}
Cool, the whole thing, written in the curly braces {} including the curly braces is called a document in MongoDB, and each property i.e name, email, and degree are called fields of that document. The collection is what contains single or multiple documents. Note: A database can contain multiple collections and these collections can further have multiple documents.
Suppose you are creating a database for a school. So, on that database, you first created a collection on which you are storing the data of each student in form of documents. Again you made another collection to store data of teachers teaching in that school. Another collection for staff and now you can extend this.
Now you have understood the basic terminology of MongoDB. Now, let's jump to the MongoDB shell.
Introducing Mongo Shell
So, if you have followed the video for the installation purpose, you will not face any problem in running the MongoDB shell from your terminal.
For starting Mongo Shell, just open your terminal and type mongo, and press enter. You will see an interface like this. If you can this interface congo you have successfully installed your mongo db.
Running our first mongo command
- To check how many databases you have right now in your mongo server
show dbs
For you, it will some only admin, local, and config, if you haven't prepared any database yet.
- To clean your shell
cls
- To create a new database and to switch into that base use the command:
use <Your-database-name>
If the named database is present in your system, it will simply switch to it. Otherwise, if the database is not present then it will create that database and get switched to it.
After running the above use database command, db is now an alias for your database name. You can simply type db to check which database you are currently working on. Okay, cool if you don't know what alias is? You can think of it as a short name ("db") for your database name. That's why type db gives your database name.
CURD Begins
Okay! Till now we have created a database. Now let's create a new collection into that database. Happy to share that we are gonna see the first operation CURD ("create", "update", "read", "delete").
To create a new collection into your database, you simply have to write the below command.
db.createCollection(<collection-name-you-want-to-give>)
Remember we saw a command like show dbs to show the databases are present. Yes, you guessed it right. Now we have a command show collections to check what collections we have in our present database.
show collections
We have created the database and then collections. Let's put some documents in that collection. For inserting documents we have 2 methods and they are:
- .insertOne( {...} )
- .insertMany( [ {...}, {...}, {...}, .... {...} ] )
Congrats, you had inserted the documents into the collections and made your first create operation.
Let's check those documents. ( Read Operation )
- .find()
- .find().pretty() // shows the documents in a nice format
You can find a specific document bypassing the query in the find method. It generally takes two parameters first one is the query and the next one is the what to show and whatnot. It will be more clear with an example.
.find( {query}, {what-to-show} )
Suppose you want to show that document whose name is Avinash, then you will pass that in the query parameter.
Now it's time to move to the update part in the CRUD operation. To update a document we have two methods and both of them take two parameters. The first one says whom to update and the second says what to update.
- .updateOne({}, {})
- .updateMany({}, {})
Suppose I have to change the roll to 78 of that person whose name is 'Avinash'. Then we will do it like this.
db.studentsInfo.updateOne({name : "Avinash"}, {$set : {roll : 78} })
Now if you will observe clearly the roll no. is updated to 78.
Now updateOne has updateMany has no difference. As the name suggests updateOne will update only the first matched query while updateMany will update all documents whose query will match.
Now let's see how to delete a field in a document. Simply use $unset in place of $set to delete the field.
db.studentsInfo.updateOne({name : "Avinash"}, {$unset : {roll : 78} })
Now we are at the end of this documentation. We will see how to delete a whole document. Again we have two methods.
- deleteOne( {query} )
- deleteMany( {query} )
Yes pretty doesn't work with pretty, I made that mistake while writing the code. ๐
So, we have done all the operation that comes under CRUD.
And it's time to say goodbye till then keep learning and keep experimenting with all these commands. In the end Thanks for reading this post. If you liked give a reaction and do comment on what I missed so that I can improve myself.