Skip to main content

CRUD Operations with MongoDB

CRUD operations indicates to the basic Insert, Read, Update and Delete operations. It is the most common requirement for any database how this will input and output. Today I want to share how to do CRUD operation with mongodb database, which is fast growing NoSQL Database.

1.) How to create or open any database in mongodb

use <Database>

2.) How to list all collections

show collections

3.) Read / find / search from documents

all documents

db.emp.find()

where dept = CS

db.emp.find({Dept:"CS"})

db.emp.find({"_id" : ObjectId("53eb50ab3bcd8479d0c302e3")})

Search name only

db.EmpDetail.find
( { },  { name: 1 })

3.) Search Documents Where dept = cs and parent is pradeep

db.emp.find({Dept:"DOT", parent:"Avinash"})

4.) Search Documents where Parent is Deepak or dept is java

db.emp.find(
{
$or:[{parent:"Deepak"},{Dept:"java"}]
}
)

5.) How to update to all users

db.users.update(
    { },
    { $set: { join_date: new Date() } },
    { multi: true }
)

db.devices.update(
    { "managerEmail" : "smnjha989@gmail.com"},
    { $set: { "FullName" : "Suman Jha" } },
    { multi: true }
)

6.) How to drop column in mongodb

db.users.update(
    { },
    { $unset: { join_date: "" } },
    { multi: true }
)

7.) How to drop collection
db.users.drop()

8.) How to create index in mongodb

db.users.createIndex( { user_id: 1 } )

9 How to insert in mongodb collection

Insert single document.
db.storeOffer.insert(
{"id": 9,"shop_id": 1495,"user_id": 7032,"affilation_amount": null,"discount_position": 50000000,"shots": 5000000}
)

inserting multiple records using command separated arrays.

db.storeOffer.insert(
[
{"id": 9,"shop_id": 1495,"user_id": 7032,"affilation_amount": null,"discount_position": 50000000,"shots": 5000000},
{"id": 10,"shop_id": 1495,"user_id": 14259,"affilation_amount": null,"discount_position": 50000000,"shots": 5000000}
]
)

10.) Search name and address where name is "Sunit"

db.EmpDetail.find( {name:"Sunit"},
{name:1, address:1,_id:0}
)
db.message.find({message_type:"M"},{body:1,_id:0})

11.) Search name and address where name is not "Manoj"

db.EmpDetail.find( {name :{$ne:"Manoj"}},
{name:1, address:1,_id:0}
)

12.) Search name like "ali"

db.EmpDetail.find(
{name: /ali/
    }
)



13.) List sort by id in DESC (1/-1)

db.EmpDetail.find().sort( {_id: -1 } )

14.) count of collections

db.EmpDetail.count()
or
db.EmpDetail.find().count()
or
db.usernotifications.count({is_read:1})
db.usernotifications.count({is_read:0})

15.) Count by field

db.EmpDetail.count({name:{$exists:true}})

16.)  Count where id>2

db.EmpDetail.count( { _id: { $gt: 2 } } )

17) Distinct 

db.EmpDetail.distinct( "name" )

18.) TOP

db.users.find().limit(1)


19.) update employee address from existing address where id is 4

db.EmpDetail.update(
   { _id: 4 },
   { $set: { address: "World Tech Park, Sector 30,Gurgaon,Haryana" } },
   { multi: true }
)

20.) Looping in mongoDB

db.employees.find().forEach( function(myDoc) { print( myDoc.name+" " + myDoc.age + " " + myDoc._id);});

21.) Remove documents from collection where _id is 2

db.EmpDetail.remove({_id:2})

22.) How to do Group by & Sum in mongodb.

db.storeposts.aggregate
(
    [
        {$group :
            {_id : "$store_post_status",  count : {$sum : 1}
                }
            }
    ]
)


How to use max/maximum and min/Minimum date  in mongoDB.
For max
db.usernotifications.find().sort({date: -1}).limit(1)
For Min
db.usernotifications.find().sort({date: 1}).limit(1)

Thanks for reading
Plz dont forget to like Facebook Page.. youngdba

Comments