1#It's the equivalent of the following SQL instruction:
2# SELECT COUNT(*) FROM Table
3# GROUP BY your_field
4# HAVING COUNT(*) > N
5query = db.collection.aggregate([
6
7 {
8 "$group": { "_id": "$your_field", #GROUP BY your_field
9 "count": {"$sum":1} } #COUNT(*)
10 },
11
12 { "$match": { "count": { "$gt": N } } } #HAVING COUNT(*) > N
13])
1#equivalent of the following SQL instruction:
2# SELECT COUNT(*) FROM Table
3# GROUP BY your_field
4query = db.collection.aggregate([
5 {
6 "$group": {
7 "_id": "$your_field", #GROUP BY your_field
8 "total": {"$sum":1} #COUNT(*)
9 }
10 }
11])
1db.sales.aggregate([
2 // First Stage
3 {
4 $match : { "date": { $gte: new ISODate("2014-01-01"), $lt: new ISODate("2015-01-01") } }
5 },
6 // Second Stage
7 {
8 $group : {
9 _id : { $dateToString: { format: "%Y-%m-%d", date: "$date" } },
10 totalSaleAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } },
11 averageQuantity: { $avg: "$quantity" },
12 count: { $sum: 1 }
13 }
14 },
15 // Third Stage
16 {
17 $sort : { totalSaleAmount: -1 }
18 }
19 ])
20