0
我有一些代码,我设置了以下管道:过滤器集合,项目年&月,按年份分组&月,然后完成一个日期时间对象,如YYYY-MM-01。如何在pymongo管道内创建日期时间对象?
实例文档:
{
_id: 123456
foo: "bar"
dt: ISODate("2015-12-24T11:59:00Z")
}
示例代码:
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)
目标:
{
_id: {dt: ISODate("2015-12-01T00:00:00Z")}
totalCount: 8
}