Aggregation Builder
Build and execute MongoDB aggregation pipelines.
Overview
Sutido provides full support for MongoDB's aggregation framework, allowing you to write complex data processing pipelines directly in the query editor.
Writing Aggregation Pipelines
Use the standard aggregate() method with an array of pipeline stages:
db.orders.aggregate([
{ $match: { status: "completed" } },
{ $group: {
_id: "$customerId",
totalSpent: { $sum: "$amount" },
orderCount: { $sum: 1 }
}},
{ $sort: { totalSpent: -1 } },
{ $limit: 10 }
]) Common Pipeline Stages
$match
Filter documents, similar to find():
{ $match: { status: "active", amount: { $gt: 100 } } } $project
Reshape documents, include or exclude fields:
{ $project: {
name: 1,
email: 1,
fullName: { $concat: ["$firstName", " ", "$lastName"] }
}} $group
Group documents and perform aggregations:
{ $group: {
_id: "$category",
count: { $sum: 1 },
avgPrice: { $avg: "$price" },
maxPrice: { $max: "$price" }
}} $sort
Order the results:
{ $sort: { createdAt: -1 } } // Descending
{ $sort: { name: 1 } } // Ascending $limit and $skip
Pagination:
{ $skip: 20 },
{ $limit: 10 } $unwind
Flatten arrays into individual documents:
{ $unwind: "$items" }
// Preserve documents with empty/missing arrays
{ $unwind: { path: "$items", preserveNullAndEmptyArrays: true } } $lookup
Join with another collection:
{ $lookup: {
from: "products",
localField: "productId",
foreignField: "_id",
as: "productDetails"
}} Advanced Lookup with Pipeline
For more complex joins, use the pipeline form:
{ $lookup: {
from: "inventory",
let: { productId: "$_id" },
pipeline: [
{ $match: {
$expr: { $eq: ["$productId", "$$productId"] },
inStock: true
}},
{ $project: { quantity: 1, warehouse: 1 } }
],
as: "stockInfo"
}} $facet for Multiple Aggregations
Run multiple pipelines in parallel on the same input:
db.orders.aggregate([
{ $facet: {
"totalRevenue": [
{ $group: { _id: null, sum: { $sum: "$total" } } }
],
"byStatus": [
{ $group: { _id: "$status", count: { $sum: 1 } } }
],
"recentOrders": [
{ $sort: { createdAt: -1 } },
{ $limit: 5 },
{ $project: { _id: 1, total: 1, createdAt: 1 } }
]
}}
]) $bucket for Histograms
Group documents into ranges:
{ $bucket: {
groupBy: "$price",
boundaries: [0, 50, 100, 200, 500],
default: "500+",
output: {
count: { $sum: 1 },
products: { $push: "$name" }
}
}} Aggregation Options
Pass options as the second argument:
db.orders.aggregate(
[
{ $match: { status: "completed" } },
{ $group: { _id: "$customerId", total: { $sum: "$amount" } } }
],
{
allowDiskUse: true, // For large sorts
maxTimeMS: 60000 // Timeout in milliseconds
}
) Autocomplete Support
The IntelliShell editor provides autocomplete for aggregation operators and field paths.
Start typing $ inside a pipeline stage to see available operators.
Viewing Results
Aggregation results appear in the same result panel as regular queries. Use Table, JSON, or Tree view to inspect the output. The Document Inspector works with aggregation results too.
Next Steps
Learn about importing and exporting data.