Introduction to MongoDB and Basic CRUD Operations
Hello readers,
This article is focused on the following topics:
· What is MongoDB?
· Setup and Installation for MongoDB
· CRUD Operation in MongoShell
1) What is MongoDB?
A MongoDB is a schema less or NoSQL database. By means of ‘schema less’ is that we can store different documents having different schema inside a same collection. Now, the next question arise is that what is Collection and what is Document? Well, check below diagram:
So, in 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 name Shopping.
Now, 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 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 a same Collection. So, here in MongoDB we will consider a table as a Collection and record as a Document and we can store different type of schema document inside a same collection. So, now let’s move to the next section and let us start implementing.
2) 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: https://www.mongodb.com/
Now click Try Free button which is on top right corner and then navigate to 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 Community Server.
Now, select the database version which you want to download. (recommendation: choose current release version). Then select the OS and click on Download button.
Now, start installing the downloaded file.
Click on Next button
Again click on Next button.
Click on Complete button.
Accept the terms and conditions and click on Next button.
Click on Next Button
Finally, click on Install button and wait till it completes its installation.
Now, after installation we need to set a PATH variable in Environment section.
For this, search Environment variable in your pc and set 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 a 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 a command mongo in this new terminal to start MongoShell.
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 comes 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 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 collection we can write as follows:
cmd>db.products.find();
This will return all documents of a products collection.
And if we write cmd>db.products.find().pretty(); this will return records in prettier way(formatted).
Now if we want to add multiple records at a time then we can use 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 a same collection)
Now to check all documents are inserted or not we can use previous command again:
cmd> db.products.find().pretty();
and this will show all documents of our products collection as follows:
Here,we can also give ObjectID manually by as follows:
cmd> db.products.insertOne({name:’abc’,_id:”p01’’});
Now let’s perform UPDATE operation:
Let us change our second document (name: mno), we will change its price and also will add new property(qty) to this document.
For this we can use 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 above query if there already exists property ‘price’ or ‘qty’ then it will update its value otherwise it will add this as new property in the document.
And you can check the updated document by running the db.products.find(); command.
And similarly we can also update multiple documents. For this we can use updateMany() method.
cmd> db.products.updateMany({name:’abc’},{$set:{ qty:150}});
By running above query all the documents having ‘abc’ name will get updated.
Now it’s time to perform DELETE operation:
cmd> db.products.deleteOne({_id:’p01’});
This will delete the document having Id ‘p01’.
Here to delete multiple documents we can use deleteMany() command.
Now if you want to drop this ‘products’ Collection (table) you can use below command:
cmd> db.products.drop();
And if you want to delete entire database then command would be:
cmd> db.dropDatabase();
This will drop all the collections of your database.
So let’s rewind all the commands: