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
}