【发布时间】:2016-11-16 22:15:29
【问题描述】:
我有一个下面定义的数组,其中包含一些字符串,
arr = ["hola", "hii", "hey", "namaste"];
下面是 我在 mongodb 中的文档
{
"user_id": "Michael",
"list": ["hola", "ola", "hey", "maybe"]
}
现在我想在 mongodb 中编写一个查询,它返回所有“arr”元素,这些元素也存在于特定“user_id”的“列表”中。
所以我想要的输出是
_id: "_id" : ObjectId("5828ae4779f2e01b06bb7a61"), results: ["hola", "hey"]
我使用聚合在下面的查询中写了,但我收到错误
MongoError: FieldPath field names may not start with '$'
我的代码
arraymodel.aggregate(
{ $match: { user_id: "Michael" }},
{ $unwind: '$list'},
{ $match: {list: {$in: arr}}},
{ $group: {_id: '$_id', results: {$push: '$list.$'}}},
function(err, docs) {
if (err) {
console.log('Error Finding query results');
console.log(err);
} else {
if (docs) {
console.log('docs: ', docs);
} else {
console.log('No Arr Found');
}
}
}
);
【问题讨论】: