The Mongoose strictQuery warning, and which value to set
A 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.
Fix it fast
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 src2Match what you see, then jump to its fix
- 1
The warning appears at start-up on Mongoose 6Mongoose 6 has no explicit strictQuery choice, so it warnsFix - 2
Most queries are fine, and one needs a different settingYou only want the behaviour on one queryFix - 3
A 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
- 1
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 mongoose2Fix
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.jsConfirmed by mongoosejs.com
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 | head2Fix
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
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 mongoose2Fix
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
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix