-
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
NB: to import data in mongodb use this command line
mongoimport --db dbName --collection collectionName fileName.json
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
- $sum :
When would like to calculate a population of a state if we know already the population on cities
mongoimport --db agg --collection zips
zips.json
db.states.aggregate([ {$group: {_id:"$state", population:{$sum:"$pop"} } }])
- $avg
$min, $max, $push, $addToSet, $first, $last.
Now we would like to translate a more complicated query that combine more than one keys for grouping.
SQL Query :
select manufacturer, category, count(*) from products group by manufacturer, category
Mongo Query:
db.products.aggregate([
{$group:
{
_id: {
"manufacturer":"$manufacturer",
"category" : "$category"},
num_products:{$sum:1}
}
}
])
We have to explain that mongo db accept an object (json) in the _id field and that what we use to execute an aggregation we multiple key.