我有一些代码在其中设置了以下管道:过滤器集合、项目年和月、按年和月分组,然后以 YYYY-MM-01 之类的日期时间对象结束。
示例文档:
{
_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
}