在Mongoose中使用RegExp填充后进行查询

Querying after populate with RegExp in Mongoose

本文关键字:查询 填充 RegExp Mongoose      更新时间:2023-09-26

我需要根据应用于从mapReduce填充的模型的正则表达式来获取文档,但我无法完成:

Article.authors(function(err, model){
  model.find({ '_id.surname': /^A/ })
       .populate({ path: '_id', model: 'Author' })
       .exec(function(err, authors){
         ...
       });
});

你能帮我吗?以上内容没有显示任何内容。。。

类似这样的东西:

model.find( { '_id.surname': new RegExp('^A') } )
     .populate({ path: '_id', model: 'Author' })
     .exec(function(err, authors){
       ...
     });

这里的主要部分是/^A/是MongoDB shell特定的语法,在JavaScript中必须使用new RegExp('^A')

您确定想要关键字_id.s姓氏吗?您可能实际上只是在寻找姓氏字段吗?所以应该是

Article.authors(function(err, model){
  model.find({ 'surname': /^A/ })
     .populate({ path: '_id', model: 'Author' })
     .exec(function(err, authors){
       ...
     });
});