I am just making a database called Fruits from my app.js
and connecting the database to MongoDB using Mongoose.
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/fruitsDB", {useNewUrlParser: true});
mongoose.set('strictQuery', false);
const fruitSchema = new mongoose.Schema({
name: String,
rating: Number,
review: String
});
const Fruit = mongoose.model("Fruit", fruitSchema);
const fruit = new Fruit({
name: "Apple",
rating: 7,
review: "Taste Good"
});
fruit.save();
Whenever I try node app.js
, I am getting DeprecationWarning. Even though I tried using mongoose.set('strictQuery', true);
, the same error continues as follows:
(node:15848) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option w
ill be switched back to `false` by default in Mongoose 7. Use `mongoose.set('str
ictQuery', 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)
D:Web DevelopmentFruitsProjectnode_modulesmongooselibdriversnode-mongodb-
nativecollection.js:158
const err = new MongooseError(message);
^
MongooseError: Operation `fruits.insertOne()` buffering timed out after 10000ms
at Timeout.<anonymous> (D:Web DevelopmentFruitsProjectnode_modulesmongoo
selibdriversnode-mongodb-nativecollection.js:158:23)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7)
Node.js v18.12.1
And then the second error also continues fruits.insertOne().
Because of this, my MongoDB database is not getting updated.
test> show dbs
admin 40.00 KiB
config 108.00 KiB
local 40.00 KiB
shopDB 72.00 KiB
I just want to fix this error. But I don’t know where to fix this error. For the second part of the error, it seems like it is coming from the node_modules itself. How can I fix this error?
23
Answers
The deprecation warning doesn’t have anything to do with the error you’re receiving. Try removing the whole
mongoose.set('strictQuery', true);
line, and you’ll get the same result.Try replacing
localhost
with127.0.0.1
:Remove this warning with just one line
OR
I am learning the same course on Udemy. Hafez’s solution worked for me.
Simply replace
with
mongoose.connect(process.env.MONGO_URL);
mongoose.set('strictQuery', true);
The above will gives you a warning even if you include that
strictQuery
line.The solution is nothing but place that
strictQuery
line beforemongoose.connect
:mongoose.set('strictQuery', true);
mongoose.connect(process.env.MONGO_URL);
Then it works!
I think we’re doing the same course. I just ignore the Mongoose deprecation warning.
Solution for the first error
You can refer to the command line instruction itself. Just use the suggested line before using Mongoose.
Simply replace:
To:
I want to give a little more context to existing answers.
When the
strict
option is set to true, Mongoose will ensure that only the fields that are specified in your schema will be saved in the database, and all other fields will not be saved (if some other fields are sent).Right now, this option is enabled by default, but it will be changed in Mongoose v7 to false by default. That means that all the fields will be saved in the database, even if some of them are not specified in the schema model.
So, if you want to have strict schemas and store in the database only what is specified in you model, starting with Mongoose v7, you will have to set
strict
option to true manually.If you don’t want this, you don’t have to specify anything, since it will be set to false by default. So, you can just connect to the database and that’s it.
Use:
You can always try to change "localhost" from
to
Yes, you just need to put this line of code above all.
See Console Message
Place the
strictQuery
line before the connect line to avoid this issue:Proper Way
I had the same issue. Just make sure you import (
require
) and usedotenv
before the MongoDB connection statement. It worked for me.This warning message is indicating that the Mongoose library is currently using the "strictQuery" option and that this option will be switched back to "false" in Mongoose 7 by default. Mongoose uses this option to determine whether to enforce strict query syntax. When set to "false", Mongoose will allow query conditions to match multiple properties.
To resolve this warning, you can either set "strictQuery" to "false" in your code by using the following line:
Or, if you want to continue using strict query syntax, you can suppress this warning by setting "strictQuery" to "true":
It’s recommended to update your code in accordance with this change before Mongoose 7 is released.
Example:
Simply do the below when your connection is established.
I was facing the same problem.
Here we go,
you can find solution in this picture.
simply you need to add this code in "import section"
const app=express();
const mongoose = require(‘mongoose’);
const express= require(‘express’);
mongoose.set(‘strictQuery’, true);
This solves the Deprecation warning for me after several tries, thanks to the poster…