Skip to content

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

@tudor33sud

Description

@tudor33sud

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"

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions