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

Make orFail() throw on updateOne() and updateMany() if matchedCount === 0, not modifiedCount === 0 #11620

Closed
tudor33sud opened this issue Apr 4, 2022 · 3 comments · Fixed by #12809
Assignees
Milestone

Comments

@tudor33sud
Copy link

tudor33sud commented Apr 4, 2022

Do you want to request a feature or report a bug?
bug
What is the current behavior?

orFail helper method will throw error on update if document is matched but not changed due to the query nature

No document found for query

  _id: {
    '$in': [
      new ObjectId("614ba802c88dc50018c69782"),
      new ObjectId("6102ea9d87655a0017d72c2c"),
      new ObjectId("61db34127cd9d445a6bc391a")
    ]
  }
}" on model "User"  

This is misleading to figure it out, since document was indeed found.

If the current behavior is a bug, please provide the steps to reproduce.

await User.updateOne(
    {
      _id: userId,
    },
    {
      $set: {
        ['metrics.invites']: 0,
      },
    },
    { timestamps: false },
  ).orFail();

This would throw an error if called on a User object which already has {metrics:{invites:0}};

What is the expected behavior?

I would expect that updateOne statement won't throw error in case matchedCount === 1 in this case, because the query is made in such a way so that if called multiple times will do nothing once user has the same value set.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
NodeJS V16
"mongoose": "^6.2.8"

@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Apr 4, 2022

From the docs

// Throws if no document was updated
await Model.updateOne({ foo: 'bar' }, { name: 'test' }).orFail();

I'll mark this as a docs issue as it could be more clear that the update operation needs to update something to not throw.

@IslandRhythms IslandRhythms added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Apr 4, 2022
@vkarpov15 vkarpov15 added this to the 6.2.14 milestone Apr 14, 2022
@vkarpov15 vkarpov15 added backwards-breaking and removed docs This issue is due to a mistake or omission in the mongoosejs.com documentation labels May 6, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.3.3, 7.0 May 6, 2022
@vkarpov15
Copy link
Collaborator

We made a note about this in the docs. However, we should consider changing this behavior in v7. 2 reasons why:

  1. Intuitively it makes more sense for await Model.updateOne({ foo: 'bar' }, { name: 'test' }).orFail(); to succeed if the matched document already has { name: 'test' }
  2. It is easier to add an extra check to simulate the current behavior "fail if no documents modified" if "fail if no documents matched" is the default than it is to go the other way and get the "fail if no documents matched" semantics. If we switch, you can always get the old behavior by doing something like const res = await Model.updateOne(filter, update).orFail(); assert.ok(res.nModified);

vkarpov15 added a commit that referenced this issue May 6, 2022
…cument updated, not if no document found

Re: #11620
@tudor33sud
Copy link
Author

I think this option totally makes sense, hence my original feedback about the method. Thanks a lot for taking care of it!

@vkarpov15 vkarpov15 changed the title OrFail() method throws error when document is found Make orFail() throw on updateOne() and updateMany() if matchedCount === 0, not modifiedCount === 0 Oct 5, 2022
@IslandRhythms IslandRhythms linked a pull request Dec 16, 2022 that will close this issue
vkarpov15 added a commit that referenced this issue Dec 16, 2022
`orFail()` now throws when `matchedCount === 0`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants