I have some code where I set up the following pipeline: filter collection, project year & month, group by year & month, and then finish with a datetime object like YYYY-MM-01.
Example Document:
{
_id: 123456
foo: "bar"
dt: ISODate("2015-12-24T11:59:00Z")
}
Example Code:
from pymongo import MongoClient
db = client.testDB
posts = db.testCollection
pipeline = [
{"$match": {"foo":"bar"}},
{"$project": {
"year": {"$year": "$dt"},
"month": {"$month": "$dt"},
}
},
{"$group": {
"_id": { "dt": ??? },
"totalCount": { "$sum": 1 }
}
},
{"$out": "myResults"}
}
posts.aggregate(pipeline)
Goal:
{
_id: {dt: ISODate("2015-12-01T00:00:00Z")}
totalCount: 8
}