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

(mongoose 6) strict option in find() query doesn't work properly #10602

Closed
lorand-horvath opened this issue Aug 25, 2021 · 5 comments
Closed
Assignees
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@lorand-horvath
Copy link
Contributor

lorand-horvath commented Aug 25, 2021

Node: 12.22.5
Mongoose: 6.0.0
MongoDB Atlas: 4.4.8

Mongoose 6 introduced a breaking change regarding the behavior of find() queries, which now discard by default any fields in the filter object that aren't found in schema. So this is as if {strict: false} is supplied to the query:

await User.find({ notInSchema: 1 }, { strict: false });
// is in fact equivalent to:
await User.find();

However, if we want to get back to the strict behavior of Mongoose 5 (which didn't discard the notInSchema field and simply returned 0 results if the field was not found in schema), we would need to set {strict: true} in Mongoose 6:

await User.find({ notInSchema: 1 }, { strict: true });

But this throws an error instead of returning 0 results (Mongoose 5 simply returned 0 results). Is this error the intended behavior?

{
"operationTime": {
"$timestamp": "7000301980966977537"
},
"ok": 0,
"code": 31254,
"codeName": "Location31254",
"$clusterTime": {
"clusterTime": {
"$timestamp": "7000301980966977537"
},
"signature": {
"hash": "bse6iPJmJyMp0gyaVapVqrR9fRY=",
"keyId": {
"low": 1,
"high": 1620587518,
"unsigned": false
}
}
}
}

@lorand-horvath
Copy link
Contributor Author

The removal of strictQuery option from schema and adding strict to 2nd parameter of Model.find() has been done in #9015 but no details there... The way it is recommended to use {strict: false} option in https://mongoosejs.com/docs/migrating_to_6.html#strictquery-is-removed-and-replaced-by-strict

await User.find({ notInSchema: 1 }, { strict: false });

is quite misleading because the 2nd argument to find() should be the projection (i.e. selecting only specific fields) and not setting options. This is very confusing, considering the current documentation and details found in migrating_to_6.html

Until this bug is fixed and the documentation is properly updated, I'll have to revert to Mongoose 5... I've spent too much time trying to debug this and getting nowhere.

@IslandRhythms IslandRhythms added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Aug 25, 2021
@vkarpov15
Copy link
Collaborator

You're right, the 2nd argument to find() is a projection, not options. We'll fix the docs. Sorry for the confusion!

@vkarpov15 vkarpov15 added this to the 6.0.1 milestone Aug 25, 2021
@lorand-horvath
Copy link
Contributor Author

@vkarpov15 Thanks for clarifying! Perhaps some explanation for the new strict option should be added to the API docs for Query.prototype.setOptions(), as I can't find anything about it here: https://mongoosejs.com/docs/api.html#query_Query-setOptions

@vkarpov15
Copy link
Collaborator

@lorand-horvath done 👍

@lorand-horvath
Copy link
Contributor Author

lorand-horvath commented Aug 25, 2021

Also important to mention that removing the strictQuery option from schemas clears up the confusion, thanks!
So basically strictQuery option has been removed from schema options and replaced with the new strict model option.
Commits 0a38c79 and 9b61ff1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

3 participants