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

fix(query): avoid sending unnecessary empty projection to MongoDB server #13059

Merged
merged 1 commit into from Feb 21, 2023

Conversation

vkarpov15
Copy link
Collaborator

Fix #13050

Summary

Right now we always send projection option to the MongoDB server, even if projection is an empty object. This is a bit clunky for numerous reasons: perf, messy logs, etc. With this change, we won't send projection if it is an empty object.

Examples

No projections in below script:

'use strict';
  
const mongoose = require('mongoose');
mongoose.set('debug', true);

run().catch(err => console.log(err));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test');
  const schema = new mongoose.Schema({ name: String });
  const Test = mongoose.model('Test', schema);

  await Test.findOne();

  await Test.find();

  await Test.findOneAndUpdate({}, { name: 'bar' });
}

Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the missing tests for this, LGTM. 👍
We could test this using sinon.spy(...).

Copy link
Collaborator

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, but should have a unit test.

@vkarpov15
Copy link
Collaborator Author

Would be nice to have a test, I opened an issue to track.

@vkarpov15 vkarpov15 merged commit 7756609 into master Feb 21, 2023
@hasezoey hasezoey deleted the vkarpov15/gh-13050 branch February 21, 2023 15:27
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this pull request Feb 22, 2023
vkarpov15 added a commit that referenced this pull request Mar 6, 2023
test(query): added tests for empty projection fix (gh-13059)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid sending empty projection option with queries
4 participants