In MongoDB, is there a way to update many documents and get the documents that were modified in a single call?

225 Views Asked by At

I'm working with the Mongo Java Driver, but looking through Mongo's documentation, it doesn't look driver specific.

update(filter, update) can update multiple documents but returns a WriteResult which only provides flags/counts.

findOneAndUpdate(filter, update) returns the actual document that was modified, but it can only update one document at a time.

Is there no way to do this in one call? If not, the client would have to call find(filter), then update(filter, update), then find(...) with a new filter matching the IDs obtained in the initial find (since the update can potentially change document values that were in the initial filter).

Is there a better way?

1

There are 1 best solutions below

0
D. SM On

I am unaware of any write commands that return a cursor, which is essentially what you are asking for, nor am I seeing anything relevant in driver source.