buunguyen / mongoose-deep-populate

Mongoose plugin to enable deep population of nested models ⛺
MIT License
469 stars 44 forks source link

Nested deep populate on arrays #41

Closed JhossePaul closed 8 years ago

JhossePaul commented 8 years ago

Hi,

I think we can improve the depth of the populate function including support for nested arrays. Suppose the next scenario

A simple model referring to other collection multiple times on an array of IDs

var postSchema = new mongoose.Schema({
    comments: [{ type: mongoose.Schema.Types.ObjectId, ref: "comments" }]
});
var Post = mongoose.model("post", postSchema);

A second model with the same structure referring to the same or another collection

var commentSchema = new mongoose.Schema({
    replies: [{ type: mongoose.Schema.Types.ObjectId, ref: "replies" }]
});
var Comment = mongoose.model("comments", commentSchema);

A third model containing the deepest level of information

var replySchema = mongoose.Schema({
    // Information for the previous models
});
var Reply = mongoose.model("replies", replySchema);

Hope you can add support for this feature

EDIT: I'm terribly sorry. The solution was in fact very easy. We have to explicit provide the right path to the deepest level i.e.

Post.find().deepPopulate(comments, comments.replies).exec(function (err, posts) {
    // Here the post object is fully populated
})