我正在尝试根据条件更新我的收藏中的某些字段.
我想设置字段active,true如果条件是true和false否则
这是无条件更新
db.consent.update(
{}, //match all
{
$set: {
"active": true
}
},
{
multi: true,
}
)
Run Code Online (Sandbox Code Playgroud)
我想添加一个更新的条件,如下所示:
db.consent.update(
{},
$cond: {
if: {
$eq: ["_id", ObjectId("5714ce0a4514ef3ef68677fd")]
},
then: {
$set: {
"active": true
}
},
else: {
$set: {
"active": false
}
}
},
{
multi: true,
}
Run Code Online (Sandbox Code Playgroud)
)
根据https://docs.mongodb.org/manual/reference/operator/update-field/,没有$cond运营商进行更新.
我有什么选择将此更新作为单个命令执行?