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.

Example:

The following document is a record from the employee collection.

{
firstName: "Mike"
, lastName: "Alfa"
, email: "mikealfa@email.com"
}

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

{
firstName: "Mike"
, lastName: "Beta"
, email: "mikebeat@email.com"
, 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.

Insert:

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.

Example:

db.employee.insert(
{
firstName: "Mike"
, lastName: "Gamma"
, email: "mikegamma@email.com"
});

SQL Comparison:

INSERT INTO Employee (FirstName, LastName, Email) VALUES ('Mike', 'Gamma', 'mikegamma@email.com')

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.

db.employee.insert({
_id: 1
, firstName: "Mike"
, lastName: "Alfa"
, email: "mikealfa@email.com"
});

db.employee.insert({
firstName: "Mike"
, lastName: "Beta"
, email: "mikebeta@email.com"
});

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.


Update:

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.

db.employee.update(
{_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


Delete:

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.

db.employee.remove(
{_id: 1});

Equivalent sql query is as below.

DELETE FROM Employee
WHERE _id: 1


Conclusion:

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

Reference:

http://docs.mongodb.org/


Discussion
6 - 1 =
** 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 2413 Views
MongoDB Restore
about 3 years ago 1708 Views
MongoDB Backup
about 3 years ago 1537 Views
MongoDB Data Model
about 4 years ago 1302 Views
MongoDB Read
about 4 years ago 1573 Views