我在 mongoDb 4.0 中使用此代码,它的工作完美无缺,但我的客户端 MongoDb 是 2.6,因此不支持 $dateToString。有没有使用 $dateToString 到 mongoDb 2.6 的替代品。
使用 MongoDb 4.0:
db.getCollection('orgData').aggregate([
{$match:{'orgId' : 5} },
{$unwind :'$events.click'},
{'$project' :{'events.click' : 1}},
{$group :{
'_id' :{'$dateToString' : {format: "%Y-%m-%d",date:'$events.click.mongo_datetime'} }
,'count' : {'$sum' : 1}
}}
]);
输出 :
/* 1 */
{
"_id" : "2019-03-01",
"count" : 1427.0
}
/* 2 */
{
"_id" : "2019-02-28",
"count" : 2244.0
}
但是在 MongoDb 2.6 中,我遇到了错误:
assert: command failed: {
"errmsg" : "exception: invalid operator '$dateToString'",
"code" : 15999,
"ok" : 0
} : aggregate failed
Error: command failed: {
"errmsg" : "exception: invalid operator '$dateToString'",
"code" : 15999,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:244:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1149:12)
at (shell):1:29
2019-09-13T17:28:57.980+1000 Error: command failed: {
"errmsg" : "exception: invalid operator '$dateToString'",
"code" : 15999,
"ok" : 0
} : aggregate failed at src/mongo/shell/assert.js:13
数据库设计: