CRUD operations in MongoDB are the collection methods that create, read, update and delete documents. Each one has a form for one document and a form for many.
To create documents, insertOne() adds one document, and insertMany() takes an array and inserts each document in it. MongoDB adds an _id to any document that has none.
db.users.insertOne({name: "John", age: 30})
db.users.insertMany([
{name: "Alice", age: 25},
{name: "Bob", age: 35}
])
To read, find() returns a cursor over the matching documents, and with no filter it matches every document. findOne() returns the first match or null. A filter such as {age: {$gte: 25}} uses query operators.
db.users.find()
db.users.find({age: {$gte: 25}})
db.users.findOne({name: "John"})
To update, updateOne() changes the first match and updateMany() changes every match. Each takes a filter and an update document, and update operators such as $set change only the fields they name.
db.users.updateOne(
{name: "John"},
{$set: {age: 31}}
)
db.users.updateMany(
{age: {$lt: 30}},
{$set: {status: "young"}}
)
To delete, deleteOne() removes the first match and deleteMany() removes every match.
db.users.deleteOne({name: "John"})
db.users.deleteMany({age: {$lt: 18}})
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓