MongoDB CRUD Operations

Share via:
MongoDB CRUD Operations

1 – To create a new database called sample data.
Atlas atlas-dsrabt-shard-0 [primary] myFirstDatabase> use sample data.

2 – To create a new collection called students.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.createCollection(“students”).

3 – To insert one document in to the collection called students
db.students.insert({_id:1,name:”yash”,rollno:”y19it79″,pincode:100001})

4 – Another way to insert one document in to the collection called students.
db.students.insertOne({_id:2,name:”arjun”,rollno:”y19it81″,pincode:00002})

5 – To insert multiple documents in to the collection called students.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.students.insertMany(
[{_id:3,name:”ram”,rollno:”y19it82″,pincode:00003},
{_id:4,name:”neraja”,rollno:”y19it83″,pincode:00004},
{_id:5,name:”sai”,rollno:”y19it84″,pincode:00005},
{_id:6,name:”krishna”,rollno:”y19it85″,pincode:00006}
])

6 – To get all the documents in the collection called students.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.students.find()

7 – To get the first document in the collection named student.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.student.findOne()

8 – To get first 5 documents in the collection called students.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.students.find().limit(5)

9 – To get all the documents with the “_id” field having the value 4.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.students.find({_id:4})

10 – To get all the documents which matches the given multiple conditions.

Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.students.find({rollno:”y19it79″,pincode:1})

11 – To decorate the obtained result.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.student.find().pretty()

12 – To update a single document where rollno is used as a where condition and $set is to update the pincode.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.students.updateOne( { rollno:”y19it79″ }, { $set: { pincode: 10001 } })

13 – To update multiple documents where roll no is used as a where condition and $set is to update the pincode.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.students.updateMany( { rollno:”y19it84″ }, { $set: { pincode: 500005 } })

14 – To delete a single document whose “_id” field having the value 3.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.student.deleteOne({_id:3})

15 – To delete multiple documents whose “name” field having the string “neraja”.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.student.deleteMany({name:”neraja”})

16 – To remove all the document whose “name” field having the string called “yash”.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.student.remove({name:”yash”})
Note: It will delete all the documents (either one or many) matches with the given condition

17 – To drop the database called student.
Atlas atlas-dsrabt-shard-0 [primary] sampledata> db.student.drop()

 

For output of the above commands please check the link below.

👇👇👇👇👇👇👇👇👇
🔗https://github.com/VinodSiram/MongoDB/

 

Author    : Teja

LinkedIn : https://www.linkedin.com/in/teja-sai-nadh-reddy-tatireddy-048882201

Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates

KTExperts is always active on social media platforms.

Facebook  : https://www.facebook.com/ktexperts/
LinkedIn    : https://www.linkedin.com/company/ktexperts/
Twitter       : https://twitter.com/ktexpertsadmin
YouTube   :  https://www.youtube.com/c/ktexperts
Instagram  : https://www.instagram.com/knowledgesharingplatform

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment