const mergePipeline = [
{'$unset': "_id"},
{'$addFields' : {"Status" : "New"}},
{'$merge' : {
into: "previous",
on: "Role ID",
whenMatched: [
// compare the documents with $cmp <-- is it possible to only compare a few fields without unsetting them?
// if different replace root with "new" document
// change status to "updated"
],
whenNotMatched: "insert"
}}
];
db.current.aggregate(mergePipeline);
I have been looking at $merge and Variables in Aggregation Expressions but I am struggling to understand. What I would like to do in a very general sense is take two collections, match them on the unique “Role ID” field and see if they are exactly the same or not. If they are the same I want to update the “Status” field to “Updated”.
Where I am struggling is on the whenMatched pipeline. I am not sure how to target the “new” and “old” document for the $cmp expression. I am also not tied to this approach. I feel like $mergeObjects could be used as well. I appreciate the help.