Get the updated document for updateMany using driver-async (Java)

88 views
Skip to first unread message

Venkat Balasubramanian

unread,
Jun 7, 2018, 4:53:54 PM6/7/18
to mongodb-user
Hi,

Is it possible to make an perform an updateMany operation which returns a list of updated documents as a result? 

I am using driver-async (Java)

Regards,
Venkat

Kevin Adistambha

unread,
Jun 28, 2018, 12:12:33 AM6/28/18
to mongodb-user

Hi Venkat

Is it possible to make an perform an updateMany operation which returns a list of updated documents as a result?

I don’t believe so. Both the async and sync versions of the method returns an instance of UpdateResult class, which only returns the count of matched and modified documents. This is similar to the output of the mongo shell after an update operation.

Having said that, the update syntax requires two things: the matching part (which should match the documents to be updated) and the update part (which specifies the update to be performed), so what documents are to be updated and what the document would look like after the update are already reflected in the syntax. Alternatively, the findOneAndUpdate method allows you to return the updated document, although this method can only update one document at a time. Do you have a specific use case in mind that is achieved by returning the updated documents?

Best regards
Kevin

Venkat Balasubramanian

unread,
Jul 18, 2018, 1:11:05 AM7/18/18
to mongodb-user
Hi Kevin,

Sorry for the late reply. I was looking for a method which updates all the records which matches the filter and returns the updated document. Below is the scenario, please let me know if you have an alternate way to achieve this. :)

For a given filter query i.e. <key, value>, we have multiple matching documents. Most of the time the filter query will match exactly one document, but there are times where the filter can match multiple documents. In this scenario, I have to update all the documents which matches the filter query and read the fields of the updated documents for some logic.

But unfortunately I couldn't find a method which updates all and get the updated document. Hence I was left with using the findOneAndUpdate method, which I have to call multiple times with the right filter query. Hence I was wondering if there are any alternate methods which can help me with this scenario, by reducing the no. of calls to DB.  

Regards,
Venkat

Kevin Adistambha

unread,
Aug 8, 2018, 10:12:48 PM8/8/18
to mongodb-user

Hi Venkat

I couldn’t find a method which updates all and get the updated document. Hence I was left with using the findOneAndUpdate method, which I have to call multiple times with the right filter query. Hence I was wondering if there are any alternate methods which can help me with this scenario, by reducing the no. of calls to DB.

I understand the pain this caused you, but unfortunately there is no feature currently that can do what you want.

Having said that, I believe SERVER-714 describes the situation you’re looking for. Please watch/comment on the ticket for updates.

Best regards
Kevin

Reply all
Reply to author
Forward
0 new messages