Introduction
The database engine makes your data queries faster by organizing and improving the steps you ask it to do.
db.collection.aggregate([
{ stage1 },
{ stage2 },
...
])db.sales.aggregate([
{ $match: { status: "A" } },
{ $group: { _id: "$item", total: { $sum: "$amount" } } }
])db.sales.aggregate([
{ $sort: { date: -1 } },
{ $limit: 5 }
])db.orders.aggregate([
{ $match: { status: "shipped" } },
{ $group: { _id: "$customerId", totalSpent: { $sum: "$amount" } } },
{ $sort: { totalSpent: -1 } }
])[{$project: {name: 1, age: 1}}, {$match: {age: {$gt: 30}}}], what will MongoDB do to optimize it?[{$group: {_id: "$category", total: {$sum: "$amount"}}}, {$match: {total: {$gt: 100}}}]. It runs slowly. What is the main issue?[{$lookup: {from: "orders", localField: "_id", foreignField: "userId", as: "orders"}}, {$unwind: "$orders"}, {$match: {"orders.status": "shipped"}}, {$group: {_id: "$_id", total: {$sum: "$orders.amount"}}}]Pass the test and this pattern moves unknown