Last updated
Simple Equality Query
Find all users with the role "admin":
// MongoDB shell
db.users.find({ role: "admin" })
// Node.js driver
collection.find({ role: "admin" })
// Python pymongo
collection.find({"role": "admin"})
In the visual builder, add one condition: field = "role", operator = "equals", value = "admin". The query is generated automatically in all driver formats.
Comparison Operators
Find products with a price less than 50 and stock greater than 0:
db.products.find({
price: { $lt: 50 },
stock: { $gt: 0 }
})
Add two conditions in the builder: price with the "less than" operator and value 50, stock with "greater than" and value 0. Multiple conditions at the same level are implicitly ANDed together.
Logical OR Query
Find users who are either admins or moderators:
db.users.find({
$or: [
{ role: "admin" },
{ role: "moderator" }
]
})
In the builder, add an OR logical operator, then add two equality conditions inside it. The tree view shows the OR container with its two child conditions clearly nested.
$in Operator
Find orders with status in a specific set of values:
db.orders.find({
status: { $in: ["pending", "processing", "shipped"] }
})
Select the "in array" operator for the status field and enter the values as a comma-separated list. The builder formats the array correctly in the generated query.
Nested Document Query
Find users whose address is in a specific city using dot notation:
db.users.find({
"address.city": "New York",
"address.country": "US"
})
The builder supports dot notation field names. Enter address.city as the field name to query nested document fields. Schema inference from a sample document provides autocomplete for nested field names.
Array Query with $elemMatch
Find products that have a review with both a rating above 4 and a verified purchase:
db.products.find({
reviews: {
$elemMatch: {
rating: { $gt: 4 },
verified: true
}
}
})
Use $elemMatch when you need multiple conditions to match the same array element. Without it, the conditions could match different elements in the array.
Text Search Query
Full-text search across indexed fields:
// First create a text index
db.articles.createIndex({ title: "text", body: "text" })
// Then search
db.articles.find({
$text: { $search: "mongodb performance optimization" }
})
// With relevance score
db.articles.find(
{ $text: { $search: "mongodb performance" } },
{ score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } })
Aggregation Pipeline — Group and Count
Count orders by status:
db.orders.aggregate([
{
$group: {
_id: "$status",
count: { $sum: 1 },
total_value: { $sum: "$amount" }
}
},
{
$sort: { count: -1 }
}
])
In the aggregation pipeline builder, add a $group stage with _id set to "$status" and two accumulator fields. Add a $sort stage after it. The builder shows each stage as a separate block in the pipeline.
Aggregation Pipeline — $lookup (Join)
Join orders with user details:
db.orders.aggregate([
{
$lookup: {
from: "users",
localField: "user_id",
foreignField: "_id",
as: "user"
}
},
{
$unwind: "$user"
},
{
$project: {
order_id: 1,
amount: 1,
"user.name": 1,
"user.email": 1
}
}
])
The $lookup stage joins the orders collection with the users collection on the user_id field. $unwind flattens the resulting array. $project selects only the needed fields.
Index Suggestion
For a query filtering by user_id and sorting by created_at:
db.orders.find({ user_id: "usr_123" }).sort({ created_at: -1 })
// Builder suggests this compound index:
db.orders.createIndex({ user_id: 1, created_at: -1 })
// Explanation: The index supports both the equality filter on user_id
// and the sort on created_at, avoiding an in-memory sort operation.
Geospatial Query
Find restaurants within 1km of a location:
// Requires a 2dsphere index
db.restaurants.createIndex({ location: "2dsphere" })
// Find nearby restaurants
db.restaurants.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [-73.9857, 40.7484] // [longitude, latitude]
},
$maxDistance: 1000 // meters
}
}
})
The builder provides a map interface for selecting the center point and radius. The coordinates and distance are filled in automatically from the map selection.