Mongoose 報錯代碼 (node:10256)(node:13604)(node:13604) DeprecationWarning: Mongoose: findOneAndUpdate()

原由

在MongoDB Node.js驅動程序中有幾個棄用,Mongoose提供瞭解決這些棄用警告的選項html

緣由是由於:findOneAndUpdate()內部會使用findAndModify驅動,驅動即將被廢棄,因此彈出警告!附上官方解釋:Mongoose v5.5.8: Deprecation Warningsnode

被替換的還有下面幾個:mongoose

  • 將update()替換爲updateOne(),updateMany(),replaceOne()
  • 將remove()替換爲deleteOne()或deleteMany()
  • 將count()替換爲countDocuments(), 除非您想要計算整個集合中有多少文檔(沒有過濾器)。在後一種狀況下,使用estimatedDocumentCount()

報錯代碼

可能報錯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 } 分別對應報錯順序修復便可文檔

相關文章
相關標籤/搜索