Querying after populate in Mongoose

后端 未结 6 2079
悲&欢浪女
悲&欢浪女 2020-11-21 16:17

I\'m pretty new to Mongoose and MongoDB in general so I\'m having a difficult time figuring out if something like this is possible:

Item = new Schema({
             


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 16:51

    what you are asking for isn't directly supported but can be achieved by adding another filter step after the query returns.

    first, .populate( 'tags', null, { tagName: { $in: ['funny', 'politics'] } } ) is definitely what you need to do to filter the tags documents. then, after the query returns you'll need to manually filter out documents that don't have any tags docs that matched the populate criteria. something like:

    query....
    .exec(function(err, docs){
       docs = docs.filter(function(doc){
         return doc.tags.length;
       })
       // do stuff with docs
    });
    

提交回复
热议问题