Querying nested embedded documents with Mongoose

前端 未结 2 1754
不思量自难忘°
不思量自难忘° 2021-01-06 10:04

I\'m trying to query inside an embedded document that is nested. I\'ve attempted to \'populate\' the results but that fails.

How do I get back all of the book detail

2条回答
  •  佛祖请我去吃肉
    2021-01-06 10:39

    Deep population was added in Mongoose 3.6. https://github.com/LearnBoost/mongoose/issues/1377#issuecomment-15911192

    For your example, it would be something like:

    Owner.find().populate('shelves').exec(PopulateBooks);
    
    function PopulateBooks(err, owners) {
          if(err) throw err;
          // Deep population is here
          Book.populate(owners, { path: 'shelves.books' }).exec(callback);
    }
    

提交回复
热议问题