In my Meteor/javascript app, I am trying to update a specific MongoDB document via $addToSet
on a certain part of that document followed by $set
on a different part of that same document. There is no overlap between these two portions, so from what I read online it should be safe. But I can't seem to get the syntax correct. How can I carry out these two operations in one javascript command? The below works as two separate commands, and I assume it would be faster if they could be combined into just one write to Mongo.
Collection.update(
{_id: documentId},
{$addToSet:
{data:
{$each: newData}
}
}
);
Collection.update(
{_id: documentId},
{$set:
{lastTxn: lastTxn,
updatedAt: new Date()}
}
);
As long as they are not on the same path ( which is the one thing you cannot do ) then it is perfectly fine. The $set
and $addToSet
operations are just top level "keys" of the "update document":
Collection.update(
{ "_id": documentId },
{
"$addToSet": { "data": { "$each": newData } },
"$set": {
"lastTxn": lastTxn,
"updatedAt": new Date()
}
}
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.