我正在尝试使用$regex http://docs.mongodb.org/manual/reference/operator/regex/ within $match http://docs.mongodb.org/manual/reference/aggregation/#stage._S_match,它不返回匹配的文档。
db.collection('MyCollection', function (err, collection) {
collection.aggregate([
{ $match: { 'Code': 'Value_01', 'Field2': { $regex: '/Value_2/g' } } },
{ $project: {
_id: 1,
CodeNumber: '$Code',
FieldName2: '$Field2'
}
}
], function (err, Result_doc) {
console.log(Result_doc);
}
});
谁能告诉我哪里出了问题或者正确的语法?
I even tried with replacing the
'Field2': { $regex: /Value_2/g }
正如它在$regex
您链接到的文档,执行此操作的两种方法是:
Field2: /Value_2/g
OR
Field2: { $regex: 'Value_2', $options: 'g' }
但我也尝试了你的第二次尝试'Field2': { $regex: /Value_2/g }
这也有效。
顺便说一句,g
正则表达式选项在这种情况下没有意义,因为无论如何你只需要一个匹配。请注意,它甚至没有列在$regex
docs.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)