在MongoDB Node.js驱动程序中有几个弃用,Mongoose提供了解决这些弃用警告的选项html
缘由是由于:findOneAndUpdate()内部会使用findAndModify驱动,驱动即将被废弃,因此弹出警告!附上官方解释:Mongoose v5.5.8: Deprecation Warningsnode
被替换的还有下面几个:mongoose
可能报错1:ui
(node:10256) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
可能报错2:code
(node:13604) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
可能报错3:htm
(node:5556) DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#findandmodify
此选项会影响如下模型和查询功能。没有任何故意向后突破的更改,所以您应该可以在不更改任何代码的状况下启用此选项。rem
Model.findByIdAndDelete() Model.findByIdAndRemove() Model.findByIdAndUpdate() Model.findOneAndDelete() Model.findOneAndRemove() Model.findOneAndUpdate() Query.findOneAndDelete() Query.findOneAndRemove() Query.findOneAndUpdate()
const mongoose = require('mongoose') mongoose.connect(mongooseConnectStr, { useUnifiedTopology: true, useNewUrlParser: true, useFindAndModify: false }, () => console.log('MongoDB 链接成功!'))
{ useUnifiedTopology: true, useNewUrlParser: true, useFindAndModify: false } 分别对应报错顺序修复便可文档