【发布时间】:2014-10-16 17:56:45
【问题描述】:
我尝试使用聚合管道根据先前的管道值附加/创建新日期并保存到新集合中。(请参阅下面的管道)。但是,语法错误,我得到一个错误说
对象表达式中不允许的字段类型 Date (at 'date') // date: new Date('$_id.year', '$_id.month', '$_id.day')
我想知道如何在 mongo 聚合管道中使用我以前的年、月、日值来新建日期?基本上,在将我的 ISODate 转换为年、月和日进行分组后,我想将它们转换回 ISODate 格式。
pipeline = [{
$match: {
event: 'sample_event',
}
}, {
$project: {
_id: false,
uuid: '$properties.distinct_id',
time: '$properties.time'
}
}, {
$group: {
_id: {
year: {
$year: '$time'
},
month: {
$month: '$time'
},
day: {
$dayOfMonth: '$time'
},
uuid: '$uuid'
}
}
}, {
$group: {
_id: {
year: '$_id.year',
month: '$_id.month',
day: '$_id.day'
},
value: { $sum: 1 }
}
}, {
$sort: {
'_id.year': 1,
'_id.month': 1,
'_id.day': 1
}
}, {
$project: {
_id: {
$concat: [
{ $substr: ['$_id.year', 0, 4] },
'-',
{
$cond: [
{ $lte: [ '$_id.month', 9 ] },
{ $concat: [
'0',
{ $substr: [ '$_id.month', 0, 2 ] }
]},
{ $substr: [ '$_id.month', 0, 2 ] }
]
},
'-',
{
$cond: [
{ $lte: [ '$_id.day', 9 ] },
{ $concat: [
'0',
{ $substr: [ '$_id.day', 0, 2 ] }
]},
{ $substr: [ '$_id.day', 0, 2 ] }
]
},
]
},
date: new Date('$_id.year', '$_id.month', '$_id.day'), // errorrrr
value: 1
}
}, {
$out: 'output_collection'
}];
【问题讨论】:
标签: javascript node.js mongodb aggregation-framework mongojs