javascript - 如何将 1 添加到 MongoDB 中的值?
问题描述
使用时findOneAndUpdate
如何将值加一?在这种情况下,我想将 1 添加到 commandsUsed 对象。下面的例子:
await mongo().then(async (mongoose) => {
try {
await userSchema.findOneAndUpdate({
_id: author.id,
}, {
_id: author.id,
commandsUsed: //I want to add 1 to this Object
}, {
upsert: true,
})
解决方案
您可以使用findOne
然后添加字段,最后保存它。
const doc = await userSchema.findOne({ _id: author.id })
doc.commandsUsed++
doc.save();
推荐阅读
- android - 在Android中写入文件时如何正确使用锁?
- javascript - 本地化日期-fns
- javascript - Vuex | 能够在突变之外进行更新
- c# - 当“SizeChanged”事件被激活时,矩形不会改变它的位置
- regex - 如何使用 REGEXMATCH 匹配包含特殊字符串但不包含另一个字符串的字符串
- c# - 在 Xamarin Forms 中获取开发设备的 IPv4
- java - Java - Mockito - CompletableFutures - 测试方法的意外结果
- javascript - youtube 视频背景
- php - Yii2 中的 Symfony/进程/进程库
- java - 一个 Kafka 阶段中的两个功能。相同状态目录错误