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

findOne with typo returns arbitrary document instead of null #12305

Closed
1 task done
florianwalther-private opened this issue Aug 21, 2022 · 2 comments
Closed
1 task done
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@florianwalther-private
Copy link

Prerequisites

  • I have written a descriptive issue title

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:

const googleSignupToken = await GoogleSignupToken.findOne({ token: token }).exec();

when I accidentally filtered with { tokenId: token } (tokenId doesn't exist as a key on this schema), it returned the wrong document.

@florianwalther-private florianwalther-private added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Aug 21, 2022
@florianwalther-private
Copy link
Author

Just found the answer on Stackoverflow. Looks like we need to disable strict query. To be honest, I'm surprised about this default behavior. It can cause some pretty devastating bugs simply by adding a typo to the key of a query.

https://stackoverflow.com/questions/70456923/mongoose-findone-and-find-return-invalid-values-when-they-should-return-nul?noredirect=1&lq=1

@vkarpov15
Copy link
Collaborator

@florianwalther-private we agree re: causing serious bugs due to typos. That's why we have #11861 open for our next major release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants