如何在$ match中的mongodb聚合查询中使用$ regex
我正在尝试使用$regex内部$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);
  }
});
谁能告诉我哪里出了问题或正确的语法?
我什至尝试更换
'Field2': { $regex: /Value_2/g }回答:
就像$regex您链接到的文档中所说的那样,执行此操作的两种方法是:
Field2: /Value_2/g要么
Field2: { $regex: 'Value_2', $options: 'g' }但是我也尝试了您的第二次尝试,'Field2': { $regex: /Value_2/g }并且效果很好。
顺便说一句,g在这种情况下,正则表达式选项没有意义,因为无论如何您只需要一个匹配项。请注意,它甚至没有在$regex文档中列出。
以上是 如何在$ match中的mongodb聚合查询中使用$ regex 的全部内容, 来源链接: utcz.com/qa/430014.html








