【发布时间】:2016-09-13 09:08:24
【问题描述】:
如何将参数传递给聚合?
我正在获取参数并尝试使用 $match 运算符传递它,但查询返回空数组:
app.get('/api/:name', function(req, res){
var name = req.params.name;
console.log(name);
db.collection('coll').aggregate([{$match: {name: '$name'}}, {$unwind: { path: "$dates", includeArrayIndex: "idx" } }, { $project: { _id: 0, dates: 1, numbers: { $arrayElemAt: ["$numbers", "$idx"] }, goals: { $arrayElemAt: ["$goals", "$idx"] }, durations: { $arrayElemAt: ["$durations", "$idx"]}}}]).toArray(function(err, docs) {
if (err) {
assert.equal(null);
}
else {
console.log(docs);
res.json(docs);
}
});
})
我应该关心管道中操作员的顺序吗?
【问题讨论】:
-
试试
{$match: {name: name}}。name变量在范围内,所以我们只需使用 javascript 在查询中引用它。
标签: node.js mongodb express aggregation-framework mean-stack