javascript - 为 mongodb 中的现有值添加价值
问题描述
我有一个
{
"name": "Banana",
"quantity": 5
}
在“产品”数据库中。我需要的是在“数量”中添加(例如)200,因此“数量”最后将是 205。我是 MongoDB 和猫鼬的新手。我试过了
const result = await Product.updateOne({
'name' : req.params.name
},
{
$set: {
quantity: this.quantity+req.body.quantity
}
}
)
但它没有用。任何帮助表示赞赏
解决方案
尝试使用$inc
运算符:
const result = await Product.updateOne({
'name' : req.params.name
},
{
$inc: {
quantity: req.body.quantity
}
})
推荐阅读
- java - 我应该如何获得相关实体?
- javascript - 由于(可能)错误的 bash 脚本导致一些奇怪的错误
- c# - 从 SMO TransferData 方法调用 CREATE INDEX 120 秒后“等待操作超时”
- python - 深度学习模型没有泛化
- c++ - std::cin 限制不能输入 LPCSTR
- java - javax.jcr.UnsupportedRepositoryOperationException:/testVersionable 处的节点不可版本化
- python - 在不迭代的情况下检测 Pandas DataFrame 列中的连续重复
- azure - 从数据工厂在 Azure VM 中执行 SSIS 包
- azure-cognitive-search - 如何将字符筛选器添加到 Azure 搜索中的 Microsoft 语言分析器?
- c# - C#向XML元素添加属性将命名空间附加到元素的末尾