From 3e3dc2e140dd72de5a7b26d70577df0ea018b22a Mon Sep 17 00:00:00 2001 From: Mark Stosberg Date: Wed, 30 Oct 2024 10:18:40 -0400 Subject: [PATCH] docs: clarify strictQuery default will flip-flop in "Migrating to 6.x" Save upgrading users the headache of `strictQuery:true` problems by highlighting that the default is going to change back in Mongoose 7, so they may wish to skip this default behavior change. --- docs/migrating_to_6.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/migrating_to_6.md b/docs/migrating_to_6.md index 5f949f1366..2de092f66b 100644 --- a/docs/migrating_to_6.md +++ b/docs/migrating_to_6.md @@ -144,9 +144,16 @@ if (existingUser) {

strictQuery is now equal to strict by default

~Mongoose no longer supports a `strictQuery` option. You must now use `strict`.~ -As of Mongoose 6.0.10, we brought back the `strictQuery` option. -However, `strictQuery` is tied to `strict` by default. -This means that, by default, Mongoose will filter out query filter properties that are not in the schema. +As of Mongoose 6.0.10, we brought back the `strictQuery` option. In Mongoose 6, `strictQuery` is set to `strict` by default. This means that, by default, Mongoose will filter out query filter properties that are not in the schema. + +However, this behavior was a source of confusion in some cases, so in Mongoose 7, this default changes back to `false`. So if you want to retain the default behavior of Mongoose 5 as well as Mongoose 7 and later, you can also disable `strictQuery` globally to override: + +```javascript +mongoose.set('strictQuery', false); +``` +In a test suite, it may be useful to set `strictQuery` to `throw`, which will throw exceptions any time a query references schema that doesn't exist, which could help identify a bug in your tests or code. + +Here's an example of the effect of `strictQuery`: ```javascript const userSchema = new Schema({ name: String });