【发布时间】:2016-08-10 11:05:10
【问题描述】:
我正在尝试根据条件更新集合中的某些字段。
如果条件为true,我想将字段active设置为true,否则设置为false
这是无条件更新
db.consent.update(
{}, //match all
{
$set: {
"active": true
}
},
{
multi: true,
}
)
我想像这样添加一个更新条件:
db.consent.update(
{},
$cond: {
if: {
$eq: ["_id", ObjectId("5714ce0a4514ef3ef68677fd")]
},
then: {
$set: {
"active": true
}
},
else: {
$set: {
"active": false
}
}
},
{
multi: true,
}
)
根据https://docs.mongodb.org/manual/reference/operator/update-field/,没有$cond 运算符可供更新。
我有哪些选项可以将这个更新作为单个命令执行?
【问题讨论】: