Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

The Mongoose strictQuery warning, and which value to set

ErrorA DeprecationWarning that mentions the strictQuery option when your app starts (Mongoose 6)

Short answer

Mongoose 6 set strictQuery to the same value as strict, so query fields missing from the schema were dropped. Mongoose 7 changed the default to false. The warning asks you to choose. Call mongoose.set('strictQuery', false) before you connect, or true if you want unknown filter fields removed.

  • Applies to Mongoose 6, Mongoose 7, Mongoose 8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find which Mongoose major version you run, then search your code for an existing strictQuery setting.

    npm ls mongoose
    grep -rn "strictQuery" --include=*.js --include=*.ts src
  2. 2Match what you see, then jump to its fix

    1. 1The warning appears at start-up on Mongoose 6Mongoose 6 has no explicit strictQuery choice, so it warnsFix
    2. 2Most queries are fine, and one needs a different settingYou only want the behaviour on one queryFix
    3. 3A filter on a field that is not in the schema used to match everything and now returns an empty arrayAfter upgrading to Mongoose 7, queries on unknown fields return nothingFix

What does the error look like?

  • A DeprecationWarning that mentions the strictQuery option when your app starts (Mongoose 6)
  • find() with a field that is not in the schema returns every document (Mongoose 6) or none (Mongoose 7)

Cause 1 of 3

Mongoose 6 has no explicit strictQuery choice, so it warns

1Check

In Mongoose 6, strictQuery defaults to the value of strict, so filter properties not in the schema are removed by default. Mongoose 7 moves the default back to false.

npm ls mongoose

2Fix

Set the option once, before the first connection or model use. Use false to keep Mongoose 7 behaviour.

const mongoose = require('mongoose');
mongoose.set('strictQuery', false);

3Verify

Restart the app. The warning no longer prints.

node server.js

Confirmed by mongoosejs.com

Did this fix it?

Cause 2 of 3

You only want the behaviour on one query

1Check

The docs show strictQuery as a per-query option. The global setting is not needed if one query is the exception.

grep -rn "find(" src | head

2Fix

Pass the option in the query options argument.

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

3Verify

Run that query and check what it returns.

node -e "require('./models/user').find({ notInSchema: 1 }, null, { strictQuery: false }).then(r => console.log(r.length))"

Confirmed by mongoosejs.com

Did this fix it?

Cause 3 of 3

After upgrading to Mongoose 7, queries on unknown fields return nothing

1Check

In Mongoose 7 strictQuery is false by default. A filter on a field outside the schema is kept and sent to MongoDB, so it matches only documents that really have that field.

npm ls mongoose

2Fix

Add the field to the schema, or fix the typo in the filter. Use strictQuery true only if you want unknown fields dropped, and that is a behaviour change to test.

// option: remove unknown filter fields (the Mongoose 6 default)
mongoose.set('strictQuery', true);

3Verify

Query again with the corrected field name.

node -e "require('./models/user').find({ email: 'a@example.com' }).then(r => console.log(r.length))"

Confirmed by mongoosejs.com

Did this fix it?

How do I stop it coming back?

  • Set strictQuery explicitly in the file that creates your Mongoose connection.
  • Read the migration guide before moving to a new Mongoose major version.
  • In tests, the Mongoose 6 guide suggests the throw value, so a query on an unknown field fails loudly.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.