Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize nested populate #3812

Open
skotchio opened this issue Jan 28, 2016 · 3 comments
Open

Optimize nested populate #3812

skotchio opened this issue Jan 28, 2016 · 3 comments

Comments

@skotchio
Copy link

In the following PostSchema we have nested structure that has _author field in post itself and comments:

var PostSchema = mongoose.Schema({
    _author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
    },
    comments: {
        type: [{
            _author: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'User'
            },

            text: {
                type: String
            }
        }]
    }
});

 var PostModel = mongoose.model('Post', PostSchema);

When we fetch post and populate _author fields mongoose makes two queries:

PostModel.findById(ObjectId(...)).populate('_author comments._author').exec(callback);

Mongoose: users.find({ _id: { '$in': [ ObjectId("1") ] } }) { fields: undefined }  
Mongoose: users.find({ _id: { '$in': [ ObjectId("2") ] } }) { fields: undefined }  

It would be great performance increment if mongoose will make only one query to populate _author field:

Mongoose: users.find({ _id: { '$in': [ ObjectId("1"),  ObjectId("2")] } }) { fields: undefined }  
@vkarpov15 vkarpov15 added this to the 4.6 milestone Jan 28, 2016
@krishna-koushik
Copy link

any idea when this might get done? really hoping for this performance gain.

@vkarpov15
Copy link
Collaborator

Not really a priority atm but PRs are welcome.

@vkarpov15
Copy link
Collaborator

We did a bunch of refactoring to support this in the 5.7 branch, but it's proven to be quite tricky. The amount of refactoring we did is about as much as I'm comfortable doing for a minor release, especially since populate() is pretty confusing and this has already delayed 5.7 by a week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants