简体   繁体   中英

Querying after populate with RegExp in Mongoose

I need to get docs depending on a regex applied to model populated from mapReduce, but I can't accomplish it:

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

Can you help me? The above don't shows me anything...

Something like this:

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

The main part here is that /^A/ is a MongoDB shell specific syntax, where in JavaScript you will have to use new RegExp('^A') .

Are you sure you want the key _id.surname. Might you not be actually looking for just the surname field ? so it should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM