-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregation.js
More file actions
32 lines (26 loc) · 761 Bytes
/
aggregation.js
File metadata and controls
32 lines (26 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// aggregation example
// 1. Group Documents by a Field and Calculate Count: find the count of the orders in different status
db.orders.aggregate(
[
{$group: {_id: "$status", "count": {$sum: 1}}}
]
);
// 2. Group Document by a field and calculate sum: find the sum of the amount of the orders in different status
db.orders.aggregate(
[
{$group: {_id: "$status", "total": {$sum: "$amount"}}}
]
);
// 3. Pipeline: find the sum of the amount of the orders with different cust_id and have status "A"
db.orders.aggregate(
[
{$match: {status: "A"}},
{$group: {_id: "$cust_id", "total": {$sum: "$amount"}}}
]
);
// 4. Pipeline: find sum of the amount for those who have a status record "A"
// For you to implement
db.orders.aggregate(
[
]
);