We all needed databases for our project, and “Mongodb” is one of them. “MongoDB” is a non-relational database and it is widely used.

Suppose you are building your project using NodeJS and you want to connect to your MongoDB database there are multiple ways to achieve it but one of the easiest and widely used methods is by using the “Mongoose” NodeJS package.

What is Mongoose?

Mongoose is a package of NodeJs which is used to connect non-relational databases. Here Mongoose is widely used to communicate with MongoDB.

Mongoose contains predefined methods, making communicating with the MongoDB database easier.

Error: [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` o` if you want to prepare for this change.

This error means that this method/way will be deprecated in a future release. Mongoose gives this warning to its user so that they can adapt new methods beforehand the release of the new version.

In Simple terms, this warning is created in order to notify users of the changes in the upcoming release.

Sample code to recreate “[MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery`” error, server.js file is:

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

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

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

where Package.json file is:

{
  "name": "backend",
  "version": "1.0.0",
  "description": "Sample",
  "main": "server.js",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "mongoose": "^6.8.0"
  },
}

when you run the server.js file you get output:

(node:8392) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` o` if you want to prepare for this change. Or use `mongoose.set('strictQu

(Use `node --trace-deprecation ...` to show where the warning was create

serve at http://localhost:5500

Connection Successful

The solution to solve Mongoose Depreciation Warning:

Method 1: Set the “StrictQuery” Value

You can easily remove/ignore this warning by setting up the StrictQuery value.

You can easily suppress the Mongoose Depreciation warning by either setting it to globally or

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

mongoose.set('strictQuery', true);

set the option to false if you want to ignore it for the current :

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

mongoose.set('strictQuery', false);

Method 2: Update/Upgrade the Package

Just update your mongoose package to the latest package and Voila! it will solve your problem.

Command to upgrade the package:

npm update mongoose

Method 3: Use the latest method/functions

If you get a depreciation warning on a particular function/method call then it’s an outdated way to do it. Search on google and update your code with the latest method.

Conclusion:

The easiest way to solve the Mongoose depreciation warning is to set up strictquery the flag. If it still doesn’t work then follow method 2 and method 3.

Solve Mongoose depreciation warning easiest way. An easy way to do it is by setting up strictQuery flag to solve it.

Download the Source Code for Free

Categorized in: