我正在尝试从此JSON对象获取用户admin的总和:
{
"_id": "5a7ef9a0ce8b5c00147c1ef3",
"assessed_by": "admin",
"rating": "Sad",
"assessment_date": "2018-02-10T13:54:53.303Z"
},
{
"_id": "5a7efe6083fec3001465b369",
"assessed_by": "admin",
"rating": "Sad",
"assessment_date": "2018-02-10T14:15:01.485Z"
}
预期产出:
{
"_id" : "admin",
"count" : 2.0
}
我还想按范围对assessment_date进行排序,因此我使用了$和运算符,但它似乎不适用于Node。我的代码在这里:
const now = moment().utc();
const endDate = moment().utc().subtract(9, 'days');
model.aggregate({
$match: {
$and: [
{rating: "Sad"},
{assessment_date: {$lte: now}},
{assessment_date: {$gte: endDate}}
]
}
}, { $group:
{ _id: "admin",
count: {
$sum: 1 }
}
}, function(err, results){
console.log(results)
})
Mongo语法似乎适用于Robomongo,但在尝试Node时它并不起作用。
答案 0 :(得分:0)
您的聚合管道阶段需要在数组中。
model.aggregate( [ { <stage> }, ... ] )