-
Notifications
You must be signed in to change notification settings - Fork 0
Home
All those examples are inspired from mongo db univversity courses
We will use the products collection as an example To start we would like to translate this SQL request to mongo db :
select manufacturer, count(*) from products group by manufacturer
the result will be :
db.products.aggregate([
{$group:
{
_id:"$manufacturer",
num_products:{$sum:1}
}
}
])
A second example :
select category, count(*) from products group by category
the result will be :
db.products.aggregate([
{$group:
{
_id:"$category",
num_products:{$sum:1}
}
}
])
To Execute an aggregation query, mongo-db run through the collections that exist prior to stage of pipeline ($group) and then building a new set of documents with the _id as specified (manufacturer or category in the previous example) and then run the aggregation operator on the other fieldthat we have created (num_products in this example).
the following are the stages in the aggregation pipeline
- $project -> reshape -> 1:1
- $match -> filter -> n:1
- $group -> aggregate -> n:1
- $sort -> sort -> 1:1
- $skip -> skips -> n:1
- $limit -> limits -> n:1
- $unwind -> normalize -> 1:n
- $out -> output -> 1:1
- $redact : security operator
- $geonear : to perform location queries