With the Java Sync MongoDB Driver, how do you acertain the impact of an aggregation pipeline?

207 Views Asked by At

I have code in Java that performs an aggregation pipeline roughly:

List<Bson> aggUpdate = Arrays.asList(
   match(...),
   project(...),
   lookup(...),
   ...
   merge(...)
);
AggregateIterable<?> result = mongoClient.getDatabase(...).getCollection(...).aggregate(aggUpdate);
result.toCollection(); // this kicks off the execution of the merge

Take note: the merge stage is last in my pipeline--I'm using this query to update a document in my collection (if conditions are correct).

What I've found is that result is not helpful at all for me to be able to determine what happened (in my case it should update 0 or 1 document). Do I need to run a separate query to figure out what happened?

I've tried to use the iterator() of the AggregateIterable result, but it does something even more befuddling: it returns every document in the collection (regardless of whether it matched in the first stage or not!). Seems like a very bad / useless cursor.

1

There are 1 best solutions below

0
Robert On

Unfortunately my only answer for now is: persist a "verification value" with the aggregation query (in my case a simple timestamp), and then execute a separate query after aggregation that ensures the "verification value" is set as expected.