18

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)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
DummyData1590
  • 181
  • 1
  • 1
  • 5
  • 1
    im sorry, this is the warning => (node:21244) [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. – DummyData1590 Dec 07 '22 at 04:32
  • what is your mongoDB url can you post it to ? – Dhaval Italiya Dec 11 '22 at 08:46
  • @PeterMortensen It's a pid – OneCricketeer Jun 10 '23 at 00:08

6 Answers6

33

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.

lpizzinidev
  • 12,741
  • 2
  • 10
  • 29
1

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,
});
Dhaval Italiya
  • 386
  • 2
  • 7
0

Go to Network Access on your MongoDB page in the browser. Then click button Add IP address and add address "0.0.0.0".

It helps you to connect.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Myroslav
  • 7
  • 1
  • The deprecation notice has nothing to do with allowed clients or any server config. Also, the warning happens without using Atlas or any cloud hosted Mongo – OneCricketeer Jun 10 '23 at 00:09
0

Mongoose supports a separate strictQuery option to avoid strict mode for query filters. This is because empty query filters cause Mongoose to return all documents in the model, which can cause issues.

Provide mongoose.set('strictQuery',true); to solve your issue

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

mongoose.set('strictQuery', true);

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}`);
});
-1

I added mongoose.set('strictQuery', true) then the message went away.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    Please review *[Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/)* (e.g., *"Images should only be used to illustrate problems that* ***can't be made clear in any other way,*** *such as to provide screenshots of a user interface."*) and [do the right thing](https://stackoverflow.com/posts/75191440/edit) (it covers answers as well). Thanks in advance. – Peter Mortensen Feb 17 '23 at 01:07
  • I've removed the image since the line of code is all that's necessary... What does this add over the most upvoted answer? https://stackoverflow.com/a/74713539/2308683 – OneCricketeer Jun 10 '23 at 00:12
-2

You can try it...

const dbSetup = async () => {
  try {
    const DBURL = process.env.DB_URL;
    mongoose.set("strictQuery", false);
    mongoose.connect(DBURL, {
      useNewUrlParser: true,
      ssl: true,
      sslValidate: false,
    });
  } catch (err) {
    console.log("Databse Connection Error : " + err.message);
  }
};
Al-Mamun
  • 9
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 27 '23 at 19:15