This is an article for beginners’ who are interested in learning mongoDB. You might have been hearing terms like “NoSQL”, ”mongoDB” and ”mongoose”. Today, we will get some basic knowledge on all these and try our hands on experience on the basics of mongoDB and rather than going into the theoretical part, we will perform some simple operations which will bring us a confidence in using mongoDB.
Just Before Starting, We Will Understand a Bit on What is NOSQL?
A NoSQL (originally referring to "non SQL", "non relational" or "not only SQL") database provides a mechanism for storage and retrieval of data which is modeled in means other than the tabular relations used in relational databases.
To understand more about NoSQL, you may search for many articles. Just for your reference, you may also check this link.
What is mongoDB?
Like MS SQL Server and Oracle SqlDeveloper for NoSQL, we have mongoDB for NoSQL which is a popular open source database. It is an open source document-oriented database where unlike table, we have collections and documents instead of rows/records where document is stored in BSON format which is slightly different from JSON.
Example of a document:
Let’s Start Now
To start this, we need a mongoDB setup in our machine, first install mongoDB to our machine.
Go here and download the required setup and install it (Since we need to type a lot in command prompt, I suggest to create a folder in C:\ and install mongoDB in it rather than C:\Program Files, however it's left to you).
After installing, you will see the below files in bin folder:
Now, we need to run a mongod process, so look at the image below:
Now our mongod process is started, you can also run mongod as Windows service. Look for mongdb documentation to know about it.
So now, open another command prompt window and do as shown in the image.
To create a database, we write use <databasename>.
So now, our students database has been switched but when we write a show dbs command, our database will not be listed because we haven’t created any collection in it. See image for reference.
To show the list of available databases, we use show dbs command:
So now, let’s create a
student collection and insert a
student name and age to our
So I have created a
collection student and inserted a document to it, now we will run show dbs command.
In the above image, you can see that we have our
students database listed.
To view our collection, we run show
To view the documents, we run
You can see the “
_id” which is similar to primary key in our RDBMS.
To view our documents in more readable form, we just need to impress our mongo by saying pretty.
Now let’s insert two more documents to our
Now, we will learn another command update so we will update
Tom age to
Tom age is updated to
Now, let’s learn how to delete a document from a collection.
We will delete
john from a collection using
We also delete
Tom from collection using his
Carefully observe the commands.
Now, we will drop our
student collection has been dropped.
Robomongo is a UI representation of our mongodb, just have a look at it people who hate command prompt try it. You need to download and install after mongodb installation.
Thank you all for reading this tutorial. There will be a massive growth in NoSQL in the coming days, so start it now. There are lots of materials for mongo on the internet, but go through the reference links given below.