【发布时间】:2019-05-02 05:54:57
【问题描述】:
我有一个对象 "input" 并将像参数一样传递给查询:
var input = {
id: 1,
componentId: x,
commands: [...],
user: [...],
}
还有集合“testCollection”:
testCollection = {
id: 1,
model: {
stepComponents: [
{
componentId: x
command: [...],
user: [...],
}
]
}
}
我的问题是如何更新 "testCollection" 上的特定字段,如果它未定义或为空,则跳过更新。 这意味着如果 "input.user" 未定义/为空,则不更新 "user" 字段。
更新查询示例如下:
testCollection.update(
{
_id: objectId(input.id),
'model.stepComponents.componentId': input.componentId
},
{
'inputs': input.inputs,
'model.stepComponents.$.commands': input.commands,
'model.stepComponents.$.users': input.users,
},
{ upsert: true },
);
任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: mongodb mongodb-query