当我尝试对 mongoDB 使用以下代码时,出现此错误:
var updateDoc = {
$set: {
energy: {
$cond: {
if: { $gt: [ { $add: [ "$energy", 20 ] }, 100 ] },
then: 100,
else: { $add: [ "$energy", 20 ] }
}
}
}
}
如何解决这个问题?
最佳答案
$cond是一个聚合运算符,更新不能在这个简单查询中允许该运算符,
看看update with aggregation pipeline从 MongoDB v4.2 开始,
您只需将 updateDoc
对象包装到数组中,
var updateDoc = [
{
$set: {
energy: {
$cond: {
if: { $gt: [{ $add: ["$energy", 20] }, 100] },
then: 100,
else: { $add: ["$energy", 20] }
}
}
}
}
]
关于javascript - 我该如何修复这个 mongodb 错误? : MongoError: The dollar ($) prefixed field '$cond' in 'energy.$cond' is not valid for storage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66335817/