Mongoose: deep population (populate a populated field)

前端 未结 10 595
独厮守ぢ
独厮守ぢ 2020-12-02 08:05

I have Category model:

Category:
    ...
    articles: [{type:ObjectId, ref:\'Article\'}]

Article model contains ref to

10条回答
  •  青春惊慌失措
    2020-12-02 08:59

    Firstly, update mongoose 3 to 4 & then use the simplest way for deep population in mongoose as below :

    Suppose you have Blog schema having userId as ref Id & then in User you have some review as ref Id for schema Review. So Basically, you have three schema : 1. Blog 2. User 3. Review

    And, you have to query from blog, which user owns this blog & the user review. So you can query your result as :

    BlogModel
      .find({})
      .populate({
        path : 'userId',
        populate : {
          path : 'reviewId'
        }
      })
      .exec(function (err, res) {
    
      })
    

提交回复
热议问题