node.js 弃用警告:不推荐使用 collection.findAndModify。改用 findOneAndUpdate、findOneAndReplace 或 findOneAndDelete?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52572852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead?
提问by Sudhanshu Gaur
I am using mongoose findOneAndUpdatebut still getting the error,
我正在使用猫鼬,findOneAndUpdate但仍然出现错误,
DeprecationWarning: collection.findAndModify is deprecated. Use findOneAndUpdate, findOneAndReplace or findOneAndDelete instead.
弃用警告:不推荐使用 collection.findAndModify。请改用 findOneAndUpdate、findOneAndReplace 或 findOneAndDelete。
But I am not even using findAndModify, why is it converting my query to findAndModify?
但我什至没有使用findAndModify,为什么它将我的查询转换为findAndModify?
回答by Shivam Pandey
You need to set the option in the query useFindAndModifyto false, as mentioned in the docs.
您需要将查询中的选项设置useFindAndModify为false,如文档中所述。
(search keyword Currently supported options are)
(搜索关键字目前支持的选项是)
'useFindAndModify': true by default. Set to false to make findOneAndUpdate() and findOneAndRemove() use native findOneAndUpdate() rather than findAndModify().
'useFindAndModify':默认为真。设置为 false 以使 findOneAndUpdate() 和 findOneAndRemove() 使用本机 findOneAndUpdate() 而不是 findAndModify()。
and if you see the definition file of mongoose, where mentioned that it calls findAndModify update command.
如果你看到 mongoose 的定义文件,其中提到它调用 findAndModify 更新命令。
/**
* Issues a mongodb findAndModify update command.
* Finds a matching document, updates it according to the update arg,
passing any options,
* and returns the found document (if any) to the callback. The query
executes immediately
* if callback is passed else a Query object is returned.
*/
findOneAndUpdate(): DocumentQuery<T | null, T>;
Recently updated in the mongoose docs (Click here) for these deprecation where mentioned:
最近在 mongoose 文档(单击此处)中更新了这些弃用中提到的内容:
Mongoose's findOneAndUpdate() long pre-dates the MongoDB driver's findOneAndUpdate() function, so it uses the MongoDB driver's findAndModify() function instead.
Mongoose 的 findOneAndUpdate() 早于 MongoDB 驱动程序的 findOneAndUpdate() 函数,因此它使用 MongoDB 驱动程序的 findAndModify() 函数代替。
There are three ways or more by which you can avoid the use of FindAndModify:
您可以通过三种或更多方式避免使用FindAndModify:
- At Global level: Set the option to false.
- 在全局级别:将选项设置为 false。
// Make Mongoose use `findOneAndUpdate()`. Note that this option is `true`
// by default, you need to set it to false.
mongoose.set('useFindAndModify', false);
- At connection level: we can configure using the connection options:
- 在连接级别:我们可以使用连接选项进行配置:
mongoose.connect(uri, { useFindAndModify: false });
- At Query level:
- 在查询级别:
await ModelName.findOneAndUpdate({matchQuery},
{$set: updateData}, {useFindAndModify: false});
回答by Ikram - Ud - Daula
Change the mongoose configuration globally like this:
像这样全局更改 mongoose 配置:
mongoose.set('useFindAndModify', false);
Or pass the options in your query string like this:
或者像这样传递查询字符串中的选项:
Person.findOneAndUpdate({_id: id}, {$set: body}, {new: true, useFindAndModify: false}).then(..
You can also manage the other mongoose deprecation warnings as mention docs
您还可以管理其他猫鼬弃用警告作为提及文档
mongoose.set('useNewUrlParser', true);
mongoose.set('useCreateIndex', true);
That's it.
就是这样。
回答by Polliny
you also can pass the options at the connection with the requirement option useNewUrlParser. Look at the following -> https://mongoosejs.com/docs/deprecations.html
您还可以在与需求选项的连接处传递选项useNewUrlParser。看下面的-> https://mongoosejs.com/docs/deprecations.html
mongoose.connect(config.MONGODB_URI, { useNewUrlParser: true, useFindAndModify: false});
回答by Devesh
You have to change your connect method options to get rid of the error:
您必须更改连接方法选项才能消除错误:
mongoose.connect("mongodb://localhost/DB_Name", {
keepAlive: true,
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
});
You can use it like this.
你可以像这样使用它。
回答by Sujeewa K. Abeysinghe
Mongoose.connect(Config.database,{useUnifiedTopology: true,useNewUrlParser: true,useFindAndModify:false});
to skip all errors :-) add this on your index.js or whatever you named it. I mean main js file. ;-)
跳过所有错误:-) 将此添加到您的 index.js 或您命名的任何内容中。我的意思是主要的js文件。;-)

