Skip to content

Instantly share code, notes, and snippets.

@lakshay-arora
Created August 26, 2020 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lakshay-arora/1db1369c3500287580baad10eba757a3 to your computer and use it in GitHub Desktop.
Save lakshay-arora/1db1369c3500287580baad10eba757a3 to your computer and use it in GitHub Desktop.
result_1 = weekly_demand_collection.aggregate([
## stage 1
{
"$match" : {
"center_id" : {
"$eq" : 11
}
}
},
## stage 2
{
"$count" : "total_rows"
}
])
for i in result_1:
print(i)
result_2 = weekly_demand_collection.aggregate([
## stage 1
{
"$match" : {
"center_id" : {
"$eq" : 11
}
}
},
## stage 2
{
"$match" : {
"checkout_price" : {
"$gt" : 130, "$lt" : 140
}
}
},
## stage 3
{
"$count" : "total_rows"
}
])
for i in result_2:
print(i)
result_3 = weekly_demand_collection.aggregate([
## stage 1
{
"$match" : {
"center_id" : {
"$eq" : 11
}
}
},
## stage 2
{
"$group" : { "_id" : 0,
"average_checkout_price" : {
"$avg" : "$checkout_price"
}
}
}
])
for i in result_3:
print(i)
result_4 = weekly_demand_collection.aggregate([
## stage 1
{
"$match" : {
"center_id" : {
"$eq" : 11
}
}
},
## stage 2
{
"$group" : { "_id" : 0,
"average_checkout_price" : {
"$avg" : "$checkout_price"
},
"total_checkout_price" : {
"$sum" : "$checkout_price"
}
}
}
])
for i in result_4:
print(i)
result_5 = weekly_demand_collection.aggregate([
## stage 1
{
"$match" : {
"center_id" : {
"$eq" : 11
}
}
},
## stage 2
{
"$group" : { "_id" : "$homepage_featured" }
}
])
for i in result_5:
print(i)
result_6 = weekly_demand_collection.aggregate([
## stage 1
{
"$match" : {
"center_id" : {
"$eq" : 11
}
}
},
## stage 2
{
"$group" : { "_id" : "$homepage_featured",
"average_checkout_price" : {
"$avg" : "$checkout_price"
}
}
}
])
for i in result_6:
print(i)
result_7 = weekly_demand_collection.aggregate([
## stage 1
{
"$match" : {
"center_id" : {
"$eq" : 11
}
}
},
## stage 2
{
"$group" : {
"_id" : {
"is_email_promotion" : "$emailer_for_promotion",
"is_homepage_featured" : "$homepage_featured"
},
"average_checkout_price" : {
"$avg" : "$checkout_price"
}
}
}
])
for i in result_7:
print(i)
result_8 = weekly_demand_collection.aggregate([
## stage 1
{
"$bucket" : {
"groupBy" : "$checkout_price",
"boundaries" : [ 0, 50, 100, 200, 400, 600, 800, 1000 ],
"default" : "Other",
"output" : {
"count" : { "$sum" : 1},
"average" : { "$avg" :"$num_orders" }
}
}
}
])
for i in result_8:
print(i)
result_9 = weekly_demand_collection.aggregate([
## stage 1
{
"$facet" : {
"average_checkout_price" : [
## stage 1
{
"$match" : {
"center_id" : {
"$eq" : 11
}
}
},
## stage 2
{
"$group" : {
"_id" : {
"is_email_promotion" : "$emailer_for_promotion",
"is_homepage_featured" : "$homepage_featured"
},
"average_checkout_price" : {
"$avg" : "$checkout_price"
}
}
}
],
"bucket_wise_data" : [
## stage 1
{
"$bucket" : {
"groupBy" : "$checkout_price",
"boundaries" : [ 0, 50, 100, 200, 400, 600, 800, 1000 ],
"default" : "Other",
"output" : {
"count" : { "$sum" : 1},
"average" : { "$avg" :"$num_orders" }
}
}
}
]
}
}
])
for i in result_9:
print(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment