Click here to Skip to main content
15,868,074 members
Articles / Web Development / Node.js

MongoDB/NoSQL For Beginners

Rate me:
Please Sign up or sign in to vote.
3.35/5 (8 votes)
25 Sep 2016CPOL3 min read 18.1K   11   2
MongoDB/NoSQL For Beginners

Introduction

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.

Source: Wikipedia

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:

Image 1

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:

Image 2

Now, we need to run a mongod process, so look at the image below:

Image 3

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.

Image 4

To create a database, we write use <databasename>.

Image 5

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:

Image 6

So now, let’s create a student collection and insert a student name and age to our student collection.

Image 7

So I have created a collection student and inserted a document to it, now we will run show dbs command.

Image 8

In the above image, you can see that we have our students database listed.

To view our collection, we run show collections command.

Image 9

To view the documents, we run db.<collectionname>.find().

Image 10

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.

i.e. db.<collectionname>.find().pretty()

Image 11

Now let’s insert two more documents to our student collection.

Image 12

Now, we will learn another command update so we will update Tom age to 40.

Image 13

So Tom age is updated to 40.

Now, let’s learn how to delete a document from a collection.

We will delete john from a collection using _id.

Image 14

We also delete Tom from collection using his name.

Image 15

Carefully observe the commands.

Now, we will drop our student collection.

Image 16

Yeah! Our student collection has been dropped.

Robomongo

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.

Image 17

Conclusion

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.

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Software Engineer in Euromonitor Research & Consulting|Bengaluru
I Work on microsoft technologies(C#,Asp.net,MVC,Azure),Angular.js,Angular 2,Node.js,Mongodb etc..

I believe every person is capable of learning a new technology but just he/she needs a kick-start to start with the learning of any technology so most of my articles will be targeting to help begineers.

Comments and Discussions

 
QuestionRoboMongo Pin
Member 1365496729-Apr-19 20:11
Member 1365496729-Apr-19 20:11 
Questiongood example Pin
Member 418313525-Sep-16 23:40
Member 418313525-Sep-16 23:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.