Skip to content

Instantly share code, notes, and snippets.

@Visrozar
Last active January 28, 2019 17:03
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 Visrozar/a85eab46c438b7bb64dcf915d1f7bda8 to your computer and use it in GitHub Desktop.
Save Visrozar/a85eab46c438b7bb64dcf915d1f7bda8 to your computer and use it in GitHub Desktop.
db.collection.aggregate([
{
"$project": {
"_id": 1,
"product": 1,
"day": {
"$dayOfMonth": "$time"
},
"month": {
"$month": "$time"
},
"year": {
"$year": "$time"
}
}
},
{
"$project": {
"_id": 1,
"product": 1,
"time": {
"$concat": [
{
"$substr": ["$year", 0, 4]
},
"-",
{
"$substr": ["$month", 0, 2]
},
"-",
{
"$substr": ["$day", 0, 2]
}
]
}
}
},
{
"$group": {
"_id": {
"time": "$time",
"product": "$product"
}
}
},
{
"$group": {
"_id": "$_id.time",
"count": {
"$sum": 1
}
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment