Mongoose populate embedded

后端 未结 4 2127
栀梦
栀梦 2020-12-06 09:17

I use Mongoose.js and cannot solve problem with 3 level hierarchy document.

There 2 ways to do it.

First - without refs.

C =         


        
4条回答
  •  误落风尘
    2020-12-06 09:57

    I'm late to this, but I wrote a Mongoose plugin that makes it extremely simple to perform deep model population. For your example, you can do this to populate b and c:

    A.find({}, function (err, docs) {
      A.deepPopulate(docs, 'b.c', cb)
    }
    

    You can also specify Mongoose populate options for each of the populated paths, like this:

    A.deepPopulate(docs, 'b.c', {
      b: { 
        select: 'name'
      }
    }, cb)
    

    Check out the plugin documentation for more information.

提交回复
热议问题