DeprecationWarning: Mongoose:在Mongoose 7中,`strictQuery`选项将默认切换回`false`。

47

我正在从我的 app.js 中创建一个名为 Fruits 的数据库,并使用 Mongoose 将数据库连接到 MongoDB。

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();

每当我尝试运行node app.js时,都会收到DeprecationWarning的警告。即使我尝试使用mongoose.set('strictQuery', true);,但仍然会出现以下相同的错误:
(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

接着第二个错误也出现在fruits.insertOne()

因此,我的MongoDB数据库没有被更新。

test> show dbs
admin    40.00 KiB
config  108.00 KiB
local    40.00 KiB
shopDB   72.00 KiB

我只想修复这个错误。但我不知道在哪里修复这个错误。对于错误的第二部分,似乎是来自于node_modules本身。我该如何修复这个错误?


请在寻求解决方案之前了解实际问题, 将您的代码放入 try catch 中并检查错误, 在我的情况下,我定义了约束条件并添加了数据,这就导致了错误, 所以我只需更改约束条件就可以解决问题了。 评论的教训是:如果我们严格遵守数据,可能会违反任何约束条件。 - Haseeb Ullah
1
const fruitSchema = new mongoose.Schema({ name: {type: String}, rating:{type: Number} , review: {type: String} }); 将其更改为上述代码,不会出现任何错误。 - Haseeb Ullah
关于 *"nodule_modules"*:你是不是指的 *node_modules?*(名为 "node_modules" 的文件夹)? - Peter Mortensen
据称,该信息来自Udemy课程 - Peter Mortensen
23个回答

86
mongoose.set("strictQuery", false);

mongoose.connect(process.env.MONGO_URL);

或者

mongoose.set("strictQuery", false);
mongoose.connect(process.env.MONGO_URL, () => {
  console.log("Connected to MongoDB");
});

const connectDB = async () => {
    try {
        mongoose.set('strictQuery', false);
        await mongoose.connect(db, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
        });
        console.log('MongoDB Connected...');
    } catch (err) {
        console.error(err.message);
        // make the process fail
        process.exit(1);
    }

35

我想为现有答案提供更多背景信息。

当将strict选项设置为true时,Mongoose将确保仅保存在模式中指定的字段,并且不会保存所有其他字段(如果发送了其他字段)。

目前,此选项默认已启用,但在Mongoose v7中将更改为默认值false。这意味着所有字段都将保存在数据库中,即使其中一些字段未在模式模型中指定。


因此,如果您想要严格的模式并仅将模型中指定的内容存储在数据库中,则从Mongoose v7开始,您将需要手动设置strict选项为true

mongoose.set('strictQuery', true);
mongoose.connect(Config.mongo_db_connection_string);

如果您不需要此功能,您无需指定任何内容,因为默认情况下它将被设置为 false 。所以,您只需连接到数据库即可。

mongoose.connect(Config.mongo_db_connection_string);

6
strict模式和strictQuery选项是不同的。如mongoose文档中所述:https://mongoosejs.com/docs/guide.html#strict 。Schema的strict选项默认启用,可以在Schema级别上进行更改,并且在mongoose 7版本中默认行为不会发生变化。您概述的全局设置的strictQuery选项将在mongoose 7中设置为false。 https://mongoosejs.com/docs/guide.html#strictQuery:strictQuery选项仅适用于查询过滤器。 - toinhao

5
const mongoose = require("mongoose");

mongoose.set('strictQuery', false);
mongoose.connect("mongodb://localhost:27017/fruitsDB", { useNewUrlParser: true });

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();

我们应该先使用给定的代码行,然后再使用mongoose。Connect("mongodb://localhost:27017/fruitsDB", {useNewUrlParser: true}); mongoose. Set('strictQuery', false); - MATI UL REHMAN
3
你的回答可以通过添加更多关于代码的功能和如何帮助提问者的信息来改进。 - Tyler2P

4

这个警告信息表明 Mongoose 库当前正在使用 "strictQuery" 选项,并且在 Mongoose 7 中,此选项将默认切换回 "false"。Mongoose 使用这个选项来确定是否强制执行严格的查询语法。当设置为 "false" 时,Mongoose 将允许查询条件匹配多个属性。

要解决此警告,您可以通过使用以下行将 "strictQuery" 设置为 "false" 在代码中进行设置:

mongoose.set('strictQuery', false);

或者,如果您想继续使用严格的查询语法,您可以通过将 "strictQuery" 设置为 "true" 来抑制此警告:

mongoose.set('strictQuery', true);

建议在Mongoose 7发布之前根据此更改更新您的代码。

示例:

const mongoose = require("mongoose");

mongoose.set('strictQuery', false);

mongoose.connect("mongodb://localhost:27017/test", {
    useNewUrlParser: true
});

3

只需一行代码即可消除此警告

mongoose.set('strictQuery', true);
mongoose.connect(process.env.MONGO_URL)

对我有用 你也可以尝试这个 https://dev59.com/JFEG5IYBdhLWcg3wHENp#74768921 - ZebraCoder

3

第一个错误的解决方案

您可以参考命令行指令本身。在使用Mongoose之前,只需使用建议的命令行即可。

只需替换:

mongoose.connect("mongodb://localhost:27017/fruitsDB", {useNewUrlParser: true});

致:

mongoose.set('strictQuery', false);
mongoose.connect("mongodb://localhost:27017/fruitsDB", {useNewUrlParser: true});

3

mongoose.connect(process.env.MONGO_URL);

mongoose.set('strictQuery', true);

以上代码会在包含strictQuery行时给出警告。 解决方案就是将strictQuery行放在mongoose.connect之前:

mongoose.set('strictQuery', true);

mongoose.connect(process.env.MONGO_URL);

然后它就可以工作了!


2

弃用警告与您收到的错误无关。尝试删除整个mongoose.set('strictQuery', true);行,您将获得相同的结果。

尝试使用127.0.0.1替换localhost

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

非常感谢,@Hafez兄弟。现在它完美地运行了。终于我可以访问我的MongoDB了。版本信息:MongoDB:6.0.3 Mongoose:6.8.0 - Rahul Saran
对我没有起作用,仍然收到相同的警告。 - Joseph Owigo
谢谢,它适用于mongoose 6.9.1。 - Chandresh
欢迎来到 Stack Overflow!Stack Overflow 不是一个论坛。请不要将 Stack Overflow 当作论坛使用。例如,需要更多信息的问题应该在评论中提出。提前感谢您的配合。 - Peter Mortensen

1

使用:

mongoose.set('strictQuery', false);

mongoose.connect("mongodb://localhost:27017/mydb", { useNewUrlParser: true })

0

我正在Udemy上学习相同的课程。Hafez的解决方案对我有用。

只需替换

mongoose.connect('mongodb://localhost:27017/fruitsDB', {useNewUrlParser: true});

使用

mongoose.set('strictQuery', true);
mongoose.connect('mongodb://127.0.0.1/fruitsDB');

课程的名称是什么? - Peter Mortensen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接