CRUD stands for Create, Read, Update, Delete. These are the four basic operations you perform on any database. MongoDB provides intuitive methods for each operation on its collections.
MongoDB
Beginner
12 min read
MongoDB CRUD Operations
Example
// Using MongoDB Shell (mongosh)
// CREATE - insertOne and insertMany
db.users.insertOne({
name: "Alice",
email: "alice@example.com",
age: 28
})
db.users.insertMany([
{ name: "Bob", email: "bob@example.com", age: 32 },
{ name: "Carol", email: "carol@example.com", age: 25 }
])
// READ - find
db.users.find() // all documents
db.users.find({ age: { $gte: 28 } }) // age >= 28
db.users.findOne({ email: "alice@example.com" })
db.users.find({}, { name: 1, email: 1 }) // projection
// UPDATE
db.users.updateOne(
{ email: "alice@example.com" },
{ $set: { age: 29, updatedAt: new Date() } }
)
db.users.updateMany(
{ age: { $lt: 30 } },
{ $inc: { age: 1 } } // increment age by 1
)
// DELETE
db.users.deleteOne({ email: "bob@example.com" })
db.users.deleteMany({ age: { $lt: 20 } })