This article is focused on the following topics.
- What is MongoDB?
- Setup and Installation for MongoDB
- CRUD Operation in MongoShell
What is MongoDB?
MongoDB is a schemaless or NoSQL database. The meaning of ‘schemaless’ is that we can store different documents having different schemas inside the same collection. Now, the next question arises is what a Collection is and what a Document is. Well, check the below diagram.
In a SQL-based database, the first step is to create the database. So here, in MongoDB, we also create the database first. In this example, I have created the database with the name Shopping.
Now, our next step is to create the tables (like Product table, Bill table, User table etc. in our case) but in MongoDB, these tables are known as Collection.
Now, the next step is to insert the records to the tables. In the case of SQL based database, all the tables will have same records with the same schema but in case of MongoDB, it is not compulsory to store all the records (Documents) with the same schema. We can store different documents having different schema inside the same Collection. So, here in MongoDB, we will consider a table as a Collection and record as a Document and we can store different types of schema documents inside the same collection. So now, let’s move to the next section and let us start implementing.
Setup and Installation
First, let’s see how to configure MongoDB locally to your machine.
Here are the steps to install MongoDB.
Firstly, open this URL from your browser.
Now, click the "Try Free" button which is on the top right corner and navigate to the Server tab.
Here, you can see it is offering two types of server: One is Community Server and another is Enterprise Server (which is a paid service).
In our case, we will choose the Community Server.
Now, select the database version which you want to download. (recommendation: choose current release version). Then, select the OS and click the "Download" button.
Now, start installing the downloaded file.
Click on the "Next" button.
Again, click the "Next" button.
Click the "Complete" button.
Accept the terms and conditions and click "Next".
Click "Next".
Finally, click on the "Install" button and wait until it completes the installation.
Now, after installation, we need to set a PATH variable in the Environment section. For this, search for the Environment variable in your PC and set the path as following:
C:\Program Files\MongoDB\Server\4.0\bin (your path up to bin folder)
Now, open the terminal (as administrator) from your pc and write the following command.
mongod --dbpath “C:\Program Files\MongoDB\Server\4.0\data\db”
(mongod = used to start the MongoDB server
dbpath = this path will point to your data folder)
By running this command, your server has started.
Now, keep this terminal running and start another terminal to run the queries on MongoShell. Write the following command in this new terminal to start MongoShell.
cmd> mongo
After this, we will write down our first query.
cmd> Show dbs
This query will display all the databases of your machine. These are default databases which come with mongoDB.
Now, we will create our own database first.
cmd> use shopping
Here, “shopping” is our database name. And now, inside this database, we need to create our Collection (tables).
Syntax: db.<collection_name>.insertOne(object)
cmd> db.products.insertOne({name:’abc’,price:30,color:’red’});
(insertOne = to insert new document(record) in the collection)
After successful completion of this query, it will return ID and this ID will be useful to fetch that document uniquely from the collection.
Now, to fetch the document from the collection, we can write like below.
cmd>db.products.find();
This will return all the documents of a products collection.
And if we write cmd>db.products.find().pretty(); this will return records in a prettier way (formatted).
Now, if we want to add multiple records at a time, then we can use the insertMany() function.
cmd>db.products.insertMany([{name:'mno',price:50},{name:'pqr',price:100,qty:1}]);
(note that in MongoDB, we can use two different schema documents inside the same collection).
Now, to check if all documents are inserted or not, we can use the previous command again.
cmd> db.products.find().pretty();
This will show all documents of our products collection as shown below.
Here,we can also give ObjectID manually.
cmd> db.products.insertOne({name:’abc’,_id:”p01’’});
Now, let’s perform the UPDATE operation.
Let us change our second document (name: mno), we will change its price and also will add a new property (qty) to this document.
For this, we can use the updateOne() method. You need to pass document’s Id or its name as its first argument and then use ‘$SET’ to update or add any property of that document.
cmd> db.products.updateOne({name:’mno’},{$set:{price:100,qty:10}});
By running the above query if there already exists property ‘price’ or ‘qty’, then it will update its value otherwise it will add this as a new property in the document.
And you can check the updated document by running the db.products.find(); command. Similarly, we can also update multiple documents. For this, we can use the updateMany() method.
cmd> db.products.updateMany({name:’abc’},{$set:{ qty:150}});
By running the above query, all the documents having ‘abc’ name will get updated.
Now, it’s time to perform the DELETE operation.
cmd> db.products.deleteOne({_id:’p01’});
This will delete the document having Id ‘p01’.
Here, to delete multiple documents, we can use the deleteMany() command.
Now, if you want to drop this ‘products’ Collection (table), you can use the below command.
cmd> db.products.drop();
And if you want to delete the entire database, then the command would be -
cmd> db.dropDatabase();
This will drop all the collections of your database.
So, let’s rewind all the commands,