Tuesday, May 12, 2020

CRUD Operations

CRUD operations refer to the basic Insert, Read, Update, and Delete operations. In the previous chapter, we learned about how to create a database and drop the database in MongoDB. Now, let us learn how to perform CRUD (Create/Read/Update/Delete) operations in MongoDB.

MongoDB: Inserting a document into a collection (Create)

The command db.collection.insert() will perform an insert operation into a collection of a document.

Let us insert a document to a student collection. You must be connected to a database for doing any insert. It is done as follows:


    regNo: "3014",

    name: "Manish Yadav",

    email: "manish@abc.com"


Note that an entry has been made into the collection called studentdb.

There’s another method called db.collection.insertMany() that lets you insert multiple documents at once. Here’s the syntax:

db.collection.insertMany([ <document 1> , <document 2>, ... ])

Let’s create a studentdb collection and populate it with some actual students:


    { regNo: "3014"name: "Manish Yadav"email: "manish@abc.com"},

    { regNo: "3015"name: "Sachin Kumar"email: "sachin@abc.com"},

    { regNo: "3016"name: "Gautam Gandhi"email: "gautam@abc.com"},

    { regNo: "3017"name: "sonal Sharma"email: "sonal@abc.com"},    



MongoDB: Querying a document from a collection(Read)

To retrieve (Select) the inserted document, run the below command. The find() command will retrieve all the documents of the given collection.


"_id" : ObjectId("5ebab48b449902ce0ca86fc0")"regNo" : "3014""name" : "Test Student""course" : { "courseName" : "MCA""duration" : "3 Years" }, "address" : { "city" : "Bangalore""state" : "KA""country" : "India" } } 

NOTE: Please observe that the record retrieved contains an attribute called _id with some unique identifier value called ObjectId which acts as a document identifier.

If a record is to be retrieved based on some criteria, the find() method should be called passing parameters, then the record will be retrieved based on the attributes specified.


For Example: Let us retrieve the record from the student collection where the attribute regNo is 3014 and the query for the same is as shown below:


The pretty() method specifies the cursor object to display the Mongo query results in an easy-to-read attractive format. 


MongoDB: Updating a document in a collection (Update)

In order to update specific field values of a collection in MongoDB, run the below query.


update() method specified above will take the fieldname and the new value as an argument to update a document.

Let us update the attribute name of the collection student for the document with regNo 3014.

db.studentdb.update({"regNo": "3014" },

{$set: { "name":"Manish Kumar Yadav"} } )


MongoDB: Removing an entry from the collection(Delete)

Let us now look into the deleting an entry from a collection. In order to delete an entry from a collection, run the command as shown below:


For Example: 


Note that after running the deleteOne() method, the entry has been deleted from the student collection.

You can perform all the above CRUD operations using following methods too in combinations: