如何将“$dateToString”用于 mongo db 2.6 是否可以替代?
how to use "$dateToString" into mongo db 2.6 is any substitute?
我在本地开发机器 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
数据库设计:
在 MongoDB 2.6 中,您需要使用 date operators together with the string operator $concat
来实现所需的表达式
作为替代品。考虑以下示例:
db.getCollection('orgData').aggregate([
{ '$match': { 'orgId' : 5 } },
{ '$unwind': '$events.click' },
{ '$group' :{
'_id': {
'$concat': [
{ '$substr': [
{ '$year': '$events.click.mongo_datetime' },
0,4
] },
'-',
{ '$substr': [
{ '$month': '$events.click.mongo_datetime' },
0,2
] },
'-',
{ '$substr': [
{ '$dayOfMonth': '$events.click.mongo_datetime' },
0,2
] }
]
},
'count': { '$sum' : 1 }
} }
]);
注意:$project
pipeline stage becomes redundant if used before a $group
pipeline step since $group
将因此更改文档架构。
我在本地开发机器 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
数据库设计:
在 MongoDB 2.6 中,您需要使用 date operators together with the string operator $concat
来实现所需的表达式
作为替代品。考虑以下示例:
db.getCollection('orgData').aggregate([
{ '$match': { 'orgId' : 5 } },
{ '$unwind': '$events.click' },
{ '$group' :{
'_id': {
'$concat': [
{ '$substr': [
{ '$year': '$events.click.mongo_datetime' },
0,4
] },
'-',
{ '$substr': [
{ '$month': '$events.click.mongo_datetime' },
0,2
] },
'-',
{ '$substr': [
{ '$dayOfMonth': '$events.click.mongo_datetime' },
0,2
] }
]
},
'count': { '$sum' : 1 }
} }
]);
注意:$project
pipeline stage becomes redundant if used before a $group
pipeline step since $group
将因此更改文档架构。