MongoDB CRUD Operation

MongoDB stores data in the form of documents. All the documents are JSON, like key and value pairs. MongoDB documents are BSON document(BSON is a binary representation of JSON document.). The value of the field can be any of the BSON data types. A field can be anything, it can be a singular value(like integer or double, string) or can be a document itself or can be a array of documents. In this way we can embed a document inside a document.


The following document is a record from the employee collection.

firstName: "Mike"
, lastName: "Alfa"
, email: ""

As the MongoDB is a document based database, second row of the employee collection can be as below.

firstName: "Mike"
, lastName: "Beta"
, email: ""
, phone: "+91-(986)-(154)-7054"
, DOB: "09/28/1978"

Lets have a look at CRUD operations on MongoDB and comparison between the same process with other database.


In MongoDB insert() method creates a new record into the collection.

db.[collection name].insert() is the command to create record

In above command:

db: stands for the database(database currently being used)

collection name: the prefered collection(on relational databse it is a table) you want to insert.

insert: It is the method to create new record. It accepts a BSON document.


firstName: "Mike"
, lastName: "Gamma"
, email: ""

SQL Comparison:

INSERT INTO Employee (FirstName, LastName, Email) VALUES ('Mike', 'Gamma', '')

Like in the relational database we can add primary key in the collection. In MongoDB default primary key is _id. Either we can send the primary key(_id) from the client or we can ignore. If we pass the primary key(_id) from the client, it will use the same as primary key, otherwise it will add a autogenerated key in the _id field.

_id: 1
, firstName: "Mike"
, lastName: "Alfa"
, email: ""

firstName: "Mike"
, lastName: "Beta"
, email: ""

Below are the results of the above queries.

Mango db insert result

If we don't provide any unique _id for the document, mongoDB automatically creates the field and assigns a unique BSON ObjectId. It is a 12 Byte BSON type that guarantee uniqueness within the collection. The ObjectId id is generated on the bases of timestamp, machineID, process ID and a process-local incremental counter.


In mongoDB the update() method updating existing document. The db.[collection name].update() accepts an query to modify the data. The description of the statement same as insert.

{_id: 1}
, {$set: {phone: "09873245678"}}
, {multi: true});

The above statement update the phone no of the record. The equivalent sql query is as below.

UPDATE employee
SET phone: "09873245678"
WHERE id = 1


In mongoDB the remove() method updating existing document. The db.[collection name].remove() accepts an query to remove the record. The description of the statement same as insert.

{_id: 1});

Equivalent sql query is as below.

WHERE _id: 1


In this tutorial, I have explained how to use basic insert, update and delete. I will add more in my next articles.


25 - Five
** To prevent abusing comments from publishing, posted comments will be reviewed and then published!
 Srikant Biswal

More from me...
Auto Increment in MongoDB
about 3 years ago 2222 Views
MongoDB Restore
about 3 years ago 1526 Views
MongoDB Backup
about 3 years ago 1434 Views
MongoDB Data Model
about 3 years ago 1216 Views
MongoDB Read
about 3 years ago 1460 Views