Skip to content

Instantly share code, notes, and snippets.

@widoyo
Last active December 12, 2015 09:09
Show Gist options
  • Save widoyo/4749528 to your computer and use it in GitHub Desktop.
Save widoyo/4749528 to your computer and use it in GitHub Desktop.
# HW 5.2
db.zips.aggregate([
{$match: {$and : [{"state": {$in: ['CA', 'NY']}}]}},
{$group: {_id: "$city", total: {$sum: "$pop"}}},
{$match: {"total": {$gt: 25000}}}, {$group: {_id: 'kota', rerata: {$avg: "$total"}}}
])
{
"_id" : ObjectID(),
"name": "",
"category": "",
"manufacturer": "",
"price": 0
}
{
"_id" : ObjectID(),
"name": "",
"category": "",
"manufacturer": "",
"price": 0
}
db.orders.aggregate({
{$group: {_id: "$manufacturer", total: {$sum: "$price"}}}
})
SQL: SELECT manufacturer, SUM(price) FROM orders GROUP BY manufacturer
db.orders.aggregate([
{$group: {_id: {'manufacturer': '$manufacturer',
'category': '$category'},
total: {$sum: 1}
}
}
])
db.orders.aggregate({
{$match: {active: true}},
{$group: {_id: "$cust_id", total: {$SUM: "$price"}}}
})
SQL: SELECT manufacturer, SUM(price) FROM orders GROUP BY manufacturer, category
db.orders.aggregate({
{$unwind: "$items"}, {$group: {_id: "$cust_id", qty: {$SUM: 1}}}
})
SQL: SELECT cust_id, SUM(li.qty) FROM orders o, order
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment