Skip to content

Commit

Permalink
docs: clarify strictQuery default will flip-flop in "Migrating to 6.x"
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstos committed Oct 30, 2024
1 parent d98b2e7 commit 3e3dc2e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions docs/migrating_to_6.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,16 @@ if (existingUser) {
<h2 id="strictquery-is-removed-and-replaced-by-strict"><a href="#strictquery-is-removed-and-replaced-by-strict"><code>strictQuery</code> is now equal to <code>strict</code> by default</a></h2>

~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 });
Expand Down

0 comments on commit 3e3dc2e

Please sign in to comment.