medium
MongoDB Medium​
How to perform CURD operations from mongo shell?
How to perform CRUD operations from mongo shell?
MongoDB supports the standard CRUD (Create, Read, Update, Delete) operations for manipulating data in a database. Here are the corresponding CRUD operations available in MongoDB:
Create:
insertOne() : Creates a new document and inserts it into a collection.
db.students.insertOne({
name: "nitish",
age: 20,
email: "[email protected]",
});
insertMany() : Creates multiple documents and inserts them into a collection.
db.students.insertMany({
name: "saurabh",
age: 22,
email: "[email protected]",
});
Read:
findOne() : Retrieves a single document from a collection based on specified criteria.
db.students.find({ name: "nitish" });
find() : Retrieves documents from a collection based on specified criteria.
db.students.find();
Update:
updateOne() : Updates a single document in a collection that matches specified criteria.
db.students.updateOne(
{ name: "saurabh" },
{ $set: { age: 25, email: "[email protected]" } }
);
updateMany() : Updates multiple documents in a collection that match specified criteria.
db.students.updateMany({}, { $set: { age: 25 } });
Delete:
deleteOne() : Deletes a single document from a collection that matches specified criteria.
db.students.deleteOne({ name: "nitish" });
deleteMany() : Deletes multiple documents from a collection that match specified criteria.
db.students.deleteMany();