Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgadiraju/64c8e0ea7dcb267ed874ad6a4dd5f8bc to your computer and use it in GitHub Desktop.
Save dgadiraju/64c8e0ea7dcb267ed874ad6a4dd5f8bc to your computer and use it in GitHub Desktop.
// Count by order_status
db.orders.
aggregate([{"$group" :
{ "_id" : "$order_status", "status_count" : {"$sum" :1 }}
},
{"$project" :
{ "order_status" : "$_id" , "status_count" : 1, "_id" : 0 }
}])
// Get revenue for each order
db.order_items.find({"order_item_order_id" : 2})   
db.order_items.
aggregate([{"$group" :
{ "_id" : "$order_item_order_id", "order_revenue" :{ "$sum" : "$order_item_subtotal" }}
},
{"$project" :
{ "order_item_order_id" : "$_id", "order_revenue" : 1, "_id" : 0 }
}])
// Get revenue for order_id 2
db.order_items.
aggregate([{ "$match" : {"order_item_order_id" : 2}},
{"$group" :
{ "_id" : "$order_item_order_id", "order_revenue" : {"$sum" : "$order_item_subtotal"}}
},
{"$project" : { "order_item_order_id": "$_id", "order_revenue" : 1, "_id" : 0 }
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment