I have an object "input" and will be passed to query like a parameter:
var input = {
id: 1,
componentId: x,
commands: [...],
user: [...],
}
And the collection "testCollection":
testCollection = {
id: 1,
model: {
stepComponents: [
{
componentId: x
command: [...],
user: [...],
}
]
}
}
My question is how can I update a specific field on "testCollection" and skip the update if it is undefined or null. That means if "input.user" is undefined/null, then not update "user" field.
The update query sample is below:
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 },
);
Any help would be appreciated. Thanks.