Home > Enterprise >  DeprecationWarning: Mongoose: the `strictQuery` option w ill be switched back to `false` by default
DeprecationWarning: Mongoose: the `strictQuery` option w ill be switched back to `false` by default

Time:12-10

Am just making a Database called Fruits from my app.js and connecting the database to MongoDB from 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 am getting DeprecationWarning. Even though, I tried using mongoose.set('strictQuery', true); the same error continues as follow:

(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 Development\FruitsProject\node_modules\mongoose\lib\drivers\node-mongodb-
native\collection.js:158
          const err = new MongooseError(message);
                      ^

MongooseError: Operation `fruits.insertOne()` buffering timed out after 10000ms 
    at Timeout.<anonymous> (D:\Web Development\FruitsProject\node_modules\mongoo
se\lib\drivers\node-mongodb-native\collection.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 2nd 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. The 2nd part of error it seems like it coming from the nodule_modules itself. How can I fix this error?

CodePudding user response:

Welcome to StackOverflow.

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 with 127.0.0.1 and let me know if it works.

mongoose.connect('mongodb://127.0.0.1/fruitsDB')

Also, which version of mongoose and MongoDB are you using?

  • Related