1. MongoDB $match Aggregation
Definition
The $match stage in MongoDB's aggregation pipeline filters documents based on specified conditions. It acts like a query operator to filter documents before passing them to the next stage. This is typically used as an early stage in the pipeline to reduce the number of documents that need processing.
Algorithm 1: Basic $match Usage :-
Example 1: Simple Match Filtering
// Filter students with high math scores
db.students.aggregate([
{
$match: {
mathScore: { $gte: 90 },
grade: "A"
}
}
])
Algorithm 2: Advanced $match Operations :-
Example 2: Complex Match Conditions
// Complex matching with multiple conditions
db.orders.aggregate([
{
$match: {
$and: [
{ status: "active" },
{ orderDate: { $gte: ISODate("2023-01-01") } },
{ totalAmount: { $gt: 1000 } },
{ "customer.type": "premium" },
{ tags: { $in: ["electronics", "gadgets"] } }
]
}
}
])