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

feat(document): add $inc() helper that increments numeric paths #12115

Merged
merged 4 commits into from Jul 19, 2022

Conversation

vkarpov15
Copy link
Collaborator

Fix #11915

Summary

See #11915. Right now, there's no way to use $inc with save(). doc.num += 2 becomes a $set.

With this change, you can explicitly increment a given path, and when you save(), Mongoose will send a $inc.

Examples

const schema = new Schema({
  counter: Number
});
Test = db.model('Test', schema);

await Test.create({ counter: 0 });
const doc = await Test.findOne();
assert.strictEqual(doc.counter, 0);
const doc2 = await Test.findOne();

doc2.counter = 1;
await doc2.save();

doc.$inc('counter');
await doc.save();

const res = await Test.findById(doc);
assert.equal(res.counter, 2);

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.

One minor nitpick, otherwise LGTM. 👍

Kind of surprised by the number of scenarios we have to take into account for this to work properly.

@AbdelrahmanHafez
Copy link
Collaborator

AbdelrahmanHafez commented Jul 18, 2022

On second thought, what do you think about the following syntax? How easy would it be to support it?

document.counter.$inc(1);
document.friends[3].age.$inc(1);

@vkarpov15
Copy link
Collaborator Author

@AbdelrahmanHafez document.counter.$inc(1); is unfortunately not viable. You can't define properties on primitives, it throws an error in strict mode.

@vkarpov15
Copy link
Collaborator Author

Re: number of cases, it is mostly just because I'm trying to be more defensive about shipping new features. In the past, we've often had edge cases in new features like "oh this doesn't work with subdocuments". So this time I'm trying to cover every potential bug I can think of with tests.

Co-authored-by: Hafez <a.hafez852@gmail.com>
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.

LGTM

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.

LGTM

@vkarpov15 vkarpov15 merged commit 067e6fe into 6.5 Jul 19, 2022
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-11915 branch July 19, 2022 00:28
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.

None yet

3 participants