码迷,mamicode.com
首页 > Web开发 > 详细

Mongoose 报错代码 (node:10256)(node:13604)(node:13604) DeprecationWarning: Mongoose: findOneAndUpdate() { useUnifiedTopology: true } { useNewUrlParser: true } to MongoClient.connect.

时间:2020-04-04 11:35:27      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:serve   efi   tps   mongod   OLE   htm   nod   struct   with   

起因

在MongoDB Node.js驱动程序中有几个弃用,Mongoose提供了解决这些弃用警告的选项

原因是因为:findOneAndUpdate()内部会使用findAndModify驱动,驱动即将被废弃,所以弹出警告!附上官方解释:Mongoose v5.5.8: Deprecation Warnings

被替换的还有下面几个:

  • 将update()替换为updateOne(),updateMany(),replaceOne()
  • 将remove()替换为deleteOne()或deleteMany()
  • 将count()替换为countDocuments(), 除非您想要计算整个集合中有多少文档(没有过滤器)。在后一种情况下,使用estimatedDocumentCount()

报错代码

可能报错1:

(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:

(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:

(node:5556) DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#findandmodify

此选项会影响以下模型和查询功能。没有任何故意向后突破的更改,因此您应该能够在不更改任何代码的情况下启用此选项。

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 } 分别对应报错顺序修复即可

Mongoose 报错代码 (node:10256)(node:13604)(node:13604) DeprecationWarning: Mongoose: findOneAndUpdate() { useUnifiedTopology: true } { useNewUrlParser: true } to MongoClient.connect.

标签:serve   efi   tps   mongod   OLE   htm   nod   struct   with   

原文地址:https://www.cnblogs.com/jing-tian/p/12630614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!