MongoDB Query API
. The MongoDB Query API is a powerful and flexible way to interact with MongoDB databases.
. It allows you to perform operations like creating, reading, updating, and deleting documents in collections using JavaScript-like syntax.
The MongoDB Query API can be used in two ways:
- CRUD Operations
- Aggregation Pipelines
Aggregation Pipelines
Aggregation pipelines process data records and return computed results. Think of it like a series of steps (stages) to transform or summarize data.
Each stage transforms the documents and passes the result to the next stage.
common MongoDB Query API operations
1 . Create (Insert) Operations
. insert one
db.users.insertOne({ name: “Bob”, age: 25 })
Get Sri Paakes’s stories in your inbox
Join Medium for free to get updates from this writer.
. insert many
db.users.insertMany([
{ name: “Carol”, age: 30 },
{ name: “Dan”, age: 40 }
])
2 . Update Operations
. one
db.users.updateOne(
{ name: “Bob” },
{ $set: { age: 26 } }
)
. many
db.users.updateMany(
{ age: { $lt: 18 } },
{ $set: { status: “minor” } }
)
Query Operators Examples
- Comparison:
$eq,$ne,$gt,$gte,$lt,$lte - Logical:
$and,$or,$not,$nor - Array:
$in,$nin,$all,$elemMatch - Element:
$exists,$type
CRUD : Basic data manipulation (insert, read, update, delete)
Aggregation : Advanced data analysis and transformation







