node.jsmongodbaggregation-framework

How to know when $merge as completed in MongoDB


I have an aggregation pipeline ending with a $merge stage.

db.collection("mycoll").aggregate([..., {$merge:{into:"myview"}}])

I have to generate materialized views at run-time and keep them up-to-date with changes.

The issue I'm facing is that if the $merge has not completed yet and I try to run an update on the target collection it will only update the documents that were already merged, while the rest gets inserted with outdated values.

In order to fix this, I would need to delay or prevent updates while the merge is running.

I can't seem to find a way to know when the merge has completed.


The cursor returned by the aggregate is empty when using $merge so I can't keep track using the cursor.

Looking at currentOp doesn't seem to show the aggregate with the $merge only showing the individual updates of each document that the merge is doing.

How can I know when the merge is done ?


Solution

  • I figured out I can add a comment to my aggregation and it's propagated by Mongo in all updates that come from that aggregation in the $currentOp output.

    So I can put a comment in my $merge aggregation like this :

    db.collection("mycoll").aggregate([..., {$merge:{into:"myview"}}], comment:"mycomment"});
    

    And search for it later using $currentOp like this:

     db.getSiblingDB("admin").aggregate([{$currentOp:{}},{$match:{"command.comment":"mycomment"}},{$limit:1}]);
    

    Also turns out if you await everything the promise only resolves when Mongo finished the $merge, so I could've just awaited everything...