I have a previous and a current collection. The collections have the following fields: userID, username, lastPosted. I want to update the previous collection if anything has changed or insert any "missing" documents. Missing being documents in current by not previous.
I was looking at this answer and it got me to a point where I could find all the deltas. I think the next steps would be to grab the userIDs from the deltaCollection and merge them into the previousCollection.
db.getCollection("previousCollection").aggregate([
{ '$unset': "_id" },
{ '$project': {
'from': "previousCollection", 'doc': "$$ROOT" }
},
{ '$unionWith':
{'coll': "currentCollection",
'pipeline': [
{ '$unset': "_id" },
{ '$project': { 'from': "currentCollection", 'doc': "$$ROOT" } }
]
}
},
{ '$group':
{'_id': "$doc",
'previous': { '$sum': { '$cond': [ { '$eq': ["$from", "previousCollection"] }, 1, 0 ] } },
'current': { '$sum': { '$cond': [ { '$eq': ["$from", "currentCollection"] }, 1, 0 ] } } }},
{ '$match': { '$expr': { '$ne': ["$previous", "$current"] } } },
{ '$match': { "current": {'$eq':1}}},
{'$unset': ["current", "previous"]},
{$out: "deltaCollection"}
], {allowDiskUse: true});
What I want to be able to do is update/insert documents that need to be worked on. I think I need to do something with $mergeObjects or $replaceRoot but I am not really sure. I feel like I should be able to group the two collections on the userID and then go from there.