Skip to content

Remove single-argument findOneAndUpdate behavior #15363

Closed
@sderrow

Description

@sderrow

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

I'm curious what the motivation is for this functionality in Model.findOneAndUpdate and the equivalent functionality in Query.prototype.findOneAndUpdate. Effectively, if only one argument is supplied to findOneAndUpdate, then it'll update a random document in the collection using that single argument as the UpdateQuery, even though the first argument is supposed to be the FilterQuery.

I'm guessing there's a rationale, but it's not documented anywhere as a valid overload (as far as I could see). I myself ran into a gnarly bug in my own code because I accidentally used findOneAndUpdate when I meant to use just findOne. That meant instead of returning null when it didn't find a document matching the query, it updated a random document and returned that document.

To prevent future confusion/bugs, I propose this behavior should be deprecated and removed entirely in 9.0 as a breaking change. You could easily recreate the behavior if necessary by replacing findOneAndUpdate(arg) with findOneAndUpdate(undefined, arg) or findOneAndUpdate({}, arg), either of which would be more clear to read and align with the official mongoose documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions