Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Mongoose deprecation warning about "strictQuery"

When I start build my backend server, I get this deprecation warning, but it's showing that I'm connected to the database.

File server.js

const dotenv = require('dotenv');
const mongoose = require('mongoose');
const app = express();
dotenv.config();

mongoose
  .connect(process.env.MONGODB_URI)
  .then(() => {
    console.log('connected to db');
  })
  .catch((err) => {
    console.log(err.message);
  });

const port = process.env.PORT || 5000;
app.listen(port, () => {
  console.log(`serve at http://localhost:${port}`);
});

File package.json

{
  "name": "backend",
  "version": "1.0.0",
  "description": "backend",
  "main": "server.js",
  "scripts": {
    "start": "node server",
    "dev": "nodemon server"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "mongoose": "^6.8.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.20"
  }
}

And this is the Mongoose deprecation warning:

Screenshot of Mongoose Deprecation Warning

It shows:

[MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7. 
Use `mongoose.set('strictQuery', false);` if you want to prepare for this change. 
Or use `mongoose.set('strictQuery', true);` to suppress this warning.
(Use `node --trace-deprecation ...` to show where the warning was created)
like image 800
DummyData1590 Avatar asked Nov 28 '25 03:11

DummyData1590


2 Answers

This warning was introduced to notify users about the change that will be introduced in Mongoose 7 to the default value of strictQuery.
It's default value will be brought back to false.

You can either set the strictQuery option to true globally to suppress the warning with:

const dotenv = require('dotenv');
const mongoose = require('mongoose');
const app = express();
dotenv.config();

mongoose.set('strictQuery', true);

Or, set the flag to false if you want to override the current strictQuery behavior and prepare for the new release:

const dotenv = require('dotenv');
const mongoose = require('mongoose');
const app = express();
dotenv.config();

mongoose.set('strictQuery', false);

Either way the warning should disappear.

For more information on why strictQuery will be brought back to false by default see here.
For more information on strictQuery see here.

like image 63
lpizzinidev Avatar answered Nov 29 '25 19:11

lpizzinidev


Add mongoose.set('strictQuery', false); before connecting to the MongoDB server.

mongoose.set('strictQuery', false);
  const conn = await mongoose.connect(process.env.MONGO_URI, {
  useNewUrlParser: true,
 useUnifiedTopology: true,
});
like image 25
Dhaval Italiya Avatar answered Nov 29 '25 19:11

Dhaval Italiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!