Mongodb CRUD operations

CRUD stands for Create, Read, Update, Delete operation. Mongodb follows JSON spec which means the document stored in mongodb follows JSON like spec such as key-value pairs. A sample Mongodb document will look like as below:-
If you observe above document you will see that there is a collection of key-value pairs. In the above document name, age and city are the keys while bhaskar, 29 and delhi are values of the keys.

Creating a simple Mongodb document

A simple Mongodb document can be created by calling db.collection.insert() method. For example
first use database by command.
use database
Mongodb document creating
Mongodb document creating

Reading Mongodb document

In order to read Mongodb documents from the database you have to use db.collection.find() method. For eg db.users.find() will retrieve all the Mongodb documents in the database.
Mongodb document reading
Mongodb document reading

If you want to retrieve particular type of Mongodb documents from the database then you have to provide condition inside the find() method. For e.g.
Mongodb particular document reading
Mongodb particular document reading

Update the Mongodb documents

In order to update the Mongodb documents you have to call db.collection.update() method. For eg.
db.users.update({"name" : "bhaskar"},{$set:{"city":"Berlin"}})
In the above query two conditions have been given. The first condition searches for the Mongodb document which needs to be updated and the second condition updates the document. For setting the new values $SET operator is used.
Mongodb particular document update
Mongodb particular document updating

Delete the Mongodb document

In order to delete the documents from the database you have to call db.collection.remove() method. For e.g

The above query will remove all the Mongodb documents with city as Berlin.
Mongodb deleting particular document
Mongodb deleting particular document
In order to do bulk delete operations when the no. of Mongodb documents to be retained is quite less its advisable to use drop() method. A word of caution drop() method should be only called when you feel that dropping the collection won't hamper your business needs.