我正在尝试对 mongodb 版本 4.4.12 的 $updateOne 中的两个字段进行加法和减法 - 如何使用 $add 和 $subtract $set 这些字段?
db.collection.updateOne({
someId: "myId",
},
{
$set: {
startDate: {
$subtract: ["$startDate", 18000000] // this is 5 hours in ms
},
endDate: {
$add: ["$endDate", 18000000]
}
}
})
上面是我当前的代码,有一些名称更改。我想知道如何通过减法和加法更新文档的 startDate 和 endDate 字段,但是当我运行此命令时,我没有得到任何更改。
我已经检查了文档,我无法使用 $dateAdd 因为我使用的是 4.4.12 并且需要 5.0;同样在 updateOne 文档中,我没有看到任何加法或减法的示例。
我也收到了错误: 尝试以这种方式执行此操作时,“endDate.$add”中的美元 ($) 前缀字段“$add”对于存储无效。
。我尝试使用 $update 即使它已被弃用,但这显然不起作用。
有人看到任何明显的问题,或者有如何解决这个问题的建议吗?
db.collection.updateOne({
someId: "myId",
},
{
$set: {
startDate: {
$subtract: ["$startDate", 18000000] // this is 5 hours in ms
},
endDate: {
$add: ["$endDate", 18000000]
}
}
})
Above is the current code I have, with a few name changes. I want to know how I can update the doc's startDate and endDate fields with subtraction and addition, but when I run this I don't get any changes.
I have checked the documentation, and I can't use $dateAdd because I'm on 4.4.12 and that requires 5.0; also in the updateOne documentation I don't see any examples with addition or subtraction.
I've also gotten the error:The dollar ($) prefixed field '$add' in 'endDate.$add' is not valid for storage.
when trying to do it this way. I tried using $update even though that's deprecated, but that obviously didn't work.
Does anyone see any glaring issues, or have a recommendation of how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查询
[]
括起来,就像[{$set ..........}]
[]
Query
[]
like[{$set ..........}]
[]