top of page

CRUD Operation in Mongodb - Get Assignments Help

Updated: Jun 12, 2023

Mongodb is a document-oriented database program widely classified as a NoSQL database program.

CRUD operation refers to Create, Read, Update and Delete


CREATE

Create means creating new data sets in the database.

There are two methods for creating data in mongodb.

1. If there is to create single data set then db.collection.insertOne() method is used.

2. If there is to create multiple data sets simultaneously then db.collection.insertMany() method is used.

Example

Here we will insert one data set in users using method db.users.insertOne().

db.users.insertOne(
    
        {
           fname: "ABC",
           mname:"Kumar",
           lname:"sSingh"
           
        }
    )

To create multiple data set simultaneously in users then db.users.insertMany() will be used.

db.users.insertMany([
    
        {
           fname: "ABC",
           mname:"Kumar",
           lname:"sSingh"  
        },
        {
            fname: "ABC",
            mname:"Kumar",
            lname:"sSingh"
            
         },
         {
            fname: "ABC",
            mname:"Kumar",
            lname:"sSingh"
            
         },
         {
            fname: "ABC",
            mname:"Kumar",
            lname:"sSingh"
            
         },
         {
            fname: "ABC",
            mname:"Kumar",
            lname:"sSingh"
            
         }
    
        ])



READ

Read means retrieving data from the database. In mongodb find method helps us to read data from data base. db.collection.find().

To read all data present in database db.collection.find() method is used.

Example 1.

db.users.find()

Output

{ "_id" : ObjectId("1"), "fname" : "ABC", "mname" : "Kumar","lname":"Singh" },
{ "_id" : ObjectId("2"), "fname" : "CED", "mname" : "Kumar" ,"lname":"Sharma"},
{ "_id" : ObjectId("3"), "fname" : "EFG", "mname" : "Kumar" ,"lname":"Verma"},
{ "_id" : ObjectId("4"), "fname" : "HIJ", "mname" : "Kumar" ,"lname":"Singh"},
{ "_id" : ObjectId("5"), "fname" : "KLM", "mname" : "Kumar" ,"lname":"Singh"}

Example 2.

To read data according to condition

db.cars.find({"fname": "ABC"})

{ "_id" : ObjectId("1"), "fname" : "ABC", "mname" : "Kumar","lname":"Singh" },


UPDATE

Update method is used to modify the data present in the database.
There are three methods of updating:-
1. db.collection.updateOne()
If we have to update only one document then we have to pass parameter according to the need. It will not insert new data , it will update the existing data.

Example 1.

updateOne({"_id" : ObjectId("1")}, {$set: { "fname" : "A1B1C1"}})

Output

{ "_id" : ObjectId("1"), "fname" : "A1B1C1", "mname" : "Kumar","lname":"Singh" }

2. db.collection.updateMany()

It will update multiple fields in the document if condition meets.

updateMany({"_id" : ObjectId("1")}, {$set: { "name" : "ABC"}, $set: { "mname" : "Kr"}})

Output

{ "_id" : ObjectId("1"), "fname" : "ABC", "mname" : "Kr","lname":"Singh" }

3. db.collection.replaceOne()

It will replace the entire document and replace the old fields with the new fields.

replaceOne({"_id" : ObjectId("1")}, { "name" : "A1B1C1"})

Output

{
   "_id" : ObjectId("1"),
   "name" : "A1B1C1"
}


Delete

Delete method deletes the document from collection. There are two method of deletion.

1. db.collection.deleteOne()

It will delete the first document according to the condition meets.

Example 1.

db.users.deleteOne(

    { "name": "A1B1C1" }
)

2. db.collection.deleteOne()

It will delete all the document according to the condition meets

db.users.deleteMany(
//delete all cars having model "2013"
    { "mname": "Kumar" }
)

Are you new to React.js or Just started learning and struggling with react.js project? We have team of react.js developer to help you in react.js project, assignment and for learning react.js at affordable price.


תגובות


bottom of page