NodeJS MongoDB 在聚合管道中传递参数
NodeJS MongoDB Pass parameter in the aggregation pipeline
如何向聚合传递参数?
我正在获取参数并尝试使用 $match
运算符传递它但查询 returns 空数组:
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);
}
});
})
我应该关心管道中运算符的顺序吗?
你似乎从来没有使用过名为 name 的变量。
试试这个,将 {$match: {name: '$name'}
更改为 {$match: {name: name}
。
试试下面的代码:-
app.get('/api/:name', function(req, res){
var name = req.params.name;
var query = [{$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"]}}}];
db.collection('coll').aggregate(query).toArray(function(err, docs) {
if (err) {
assert.equal(null);
}
else {
console.log(docs);
res.json(docs);
}
});
})
Mongoose 不投射流水线阶段。除非 _id 是数据库中的字符串
,否则以下将不起作用
new Aggregate([{ $match: { _id: '00000000000000000000000a' } }]);
// Do this instead to cast to an ObjectId
new Aggregate([{ $match: { _id:
mongoose.Types.ObjectId('00000000000000000000000a') } }]);
API URL: https://mongoosejs.com/docs/api.html#aggregate_Aggregate
尝试,
{$匹配:{'name':req.params.name}}
这对我有用
如何向聚合传递参数?
我正在获取参数并尝试使用 $match
运算符传递它但查询 returns 空数组:
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);
}
});
})
我应该关心管道中运算符的顺序吗?
你似乎从来没有使用过名为 name 的变量。
试试这个,将 {$match: {name: '$name'}
更改为 {$match: {name: name}
。
试试下面的代码:-
app.get('/api/:name', function(req, res){
var name = req.params.name;
var query = [{$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"]}}}];
db.collection('coll').aggregate(query).toArray(function(err, docs) {
if (err) {
assert.equal(null);
}
else {
console.log(docs);
res.json(docs);
}
});
})
Mongoose 不投射流水线阶段。除非 _id 是数据库中的字符串
,否则以下将不起作用new Aggregate([{ $match: { _id: '00000000000000000000000a' } }]);
// Do this instead to cast to an ObjectId
new Aggregate([{ $match: { _id:
mongoose.Types.ObjectId('00000000000000000000000a') } }]);
API URL: https://mongoosejs.com/docs/api.html#aggregate_Aggregate
尝试, {$匹配:{'name':req.params.name}} 这对我有用