findOne with typo returns arbitrary document instead of null #12305
Labels
help wanted
help
This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Prerequisites
Mongoose version
6.2.9
Node.js version
16.13.2
MongoDB version
5.0.10
Operating system
Windows
Operating system version (i.e. 20.04, 11.3, 10)
10
Issue
When I use Mongoose's
findOne
with a filter that uses a non-existing key, the query returns an arbitrary document (probably the first one in the collection). This can cause some devastating bugs. Why does Mongoose behave this way and is there a way to avoid this? When the key/value combination in the filer doesn't exist, I would expect the query to return no result.Example:
when I accidentally filtered with
{ tokenId: token }
(tokenId
doesn't exist as a key on this schema), it returned the wrong document.The text was updated successfully, but these errors were encountered: