Querying after populate in Mongoose

后端 未结 6 2071
悲&欢浪女
悲&欢浪女 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:35

    Update: Please take a look at the comments - this answer does not correctly match to the question, but maybe it answers other questions of users which came across (I think that because of the upvotes) so I will not delete this "answer":

    First: I know this question is really outdated, but I searched for exactly this problem and this SO post was the Google entry #1. So I implemented the docs.filter version (accepted answer) but as I read in the mongoose v4.6.0 docs we can now simply use:

    Item.find({}).populate({
        path: 'tags',
        match: { tagName: { $in: ['funny', 'politics'] }}
    }).exec((err, items) => {
      console.log(items.tags) 
      // contains only tags where tagName is 'funny' or 'politics'
    })
    

    Hope this helps future search machine users.

提交回复
热议问题