Mongoose: deep population (populate a populated field)

前端 未结 10 590
独厮守ぢ
独厮守ぢ 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:48

    It might be a bit too late, but I wrote a Mongoose plugin to perform deep population at any arbitrary nested levels. With this plugin registered, you can populate category's articles and accounts with just a single line:

    Category.deepPopulate(categories, 'articles.account', cb)
    

    You can also specify populate options to control things like limit, select... for each populated path. Checkout the plugin documentation for more information.

提交回复
热议问题