所以我試圖更新我的數據庫中的一個字段以每次都增加它。像一個計數器,以便我不斷獲得更新的值。為此,我正在使用這個var CounterSchema = new Schema({ _id: String, _const: String, count: Number});然后我創建了我的模型const Counter = mongoose.model("counter", CounterSchema);然后我正在使用一個函數來更新我的集合中的一個字段,例如以下......async function getNextSequence() { var count = await Counter.findOneAndUpdate( { _const: "some" }, { $inc: { count: 1 } }, { new: true } ); return count.count;}但是在運行上述函數時會出現一些錯誤。我已經確定給定的 _id 存在,這是我得到的錯誤……(node:10788) DeprecationWarning: Mongoose: `findOneAndUpdate()` and `findOneAndDelete()` without the `useFindAndModify` option set to false are deprecated. See: https://mongoosejs.com/docs/deprecations.html#findandmodify9:58 PM{ ValidationError: Url validation failed: counter: Cast to Number failed for value "Promise { <pending> }" at path "counter"9:58 PMat ValidationError.inspect (/rbd/pnpm-volume/f971d4d8-b5ba-4fbb-87f2-7580f1e7da53/node_modules/.registry.npmjs.org/mongoose/5.9.7/node_modules/mongoose/lib/error/validation.js:61:24)9:58 PMat formatValue (internal/util/inspect.js:493:31)9:58 PMat inspect (internal/util/inspect.js:191:10)9:58 PMat Object.formatWithOptions (util.js:84:12)9:58 PMat Console.(anonymous function) (console.js:191:15)9:58 PMat Console.log (console.js:202:31)9:58 PMJump Toat /app/server.js:107:399:58 PMat /rbd/pnpm-volume/f971d4d8-b5ba-4fbb-87f2-7580f1e7da53/node_modules/.registry.npmjs.org/mongoose/5.9.7/node_modules/mongoose/lib/helpers/promiseOrCallback.js:16:119:58 PMat /rbd/pnpm-volume/f971d4d8-b5ba-4fbb-87f2-7580f1e7da53/node_modules/.registry.npmjs.org/mongoose/5.9.7/node_modules/mongoose/lib/model.js:4860:219:58 PMat _done (/rbd/pnpm-volume/f971d4d8-b5ba-4fbb-87f2-7580f1e7da53/node_modules/.registry.npmjs.org/mongoose/5.9.7/node_modules/mongoose/lib/model.js:3120:16)9:58 PMat fn (/rbd/pnpm-volume/f971d4d8-b5ba-4fbb-87f2-7580f1e7da53/node_modules/.registry.npmjs.org/mongoose/5.9.7/node_modules/mongoose/lib/model.js:3135:18)
使用 mongo 中的 $inc 字段進行更新會出錯
MM們
2022-06-16 10:40:43
