Description
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.