MongoRepository Save() issue

Hi Team,
Am facing an issue MongoRepository save() method. Once I updated the document by calling save() method and am trying to fetch the same document by using findById(), it will not reflected the updated value in result. could you please help on this issue asap?

1 Like

Hello @Rojan_John_V, welcome to the MongoDB community forum.

The MongoRepository#save method returns an instance of the saved entity. Can you verify that the returned object has the updated values (you can use a System.out.println(returnedEntity) immediately after the save )?

I tried such code and found querying the same collection using the MongoRepository#.findById returned the document with updated values.

I am using MongoDB v4.2.8 and Spring Boot v2.2.4.

Thanks Prasad for your reply. This issue am facing intermittently and I could see the updated values when I am printing return object from save(). If am putting some time delay between save and findbyid, I could see the expected result.

Are you able to reproduce the issue consistently?

What is happening if you dont use the time delay?

As per our architecture, the first request will fetch the document by using findbyid() method and ready to update content and save to db. After 200 or 300 milliseconds difference the second request will come and try to fetch the same document, that time I could see the document having old value.
It will happen intermittently not all time. if am setting delay between the requests it never happen, without delay it will happen sometimes.
I tried to reproduce this issue in local mongo db by triggering bulk of requests, but I could not see this issue. In dev region I could see this with concurrent requests.
In dev we are using MongoAtlas with one primary node and two secondary node.

What are your settings about the Read Preference?

We are not setting any specific readPreference value, I believe the default should be Primary. How can we check this in MongoAtlas console?

The connection url i like this
mongodb+srv://{USER}:{PASSWORD}@{HOST}/{DB_NAME}?retryWrites=true&w=majority
do we need to set readPreference along with the connection url ?

The default Read Preference is Primary. Having a setting other than Primary, can result in reading stale data. Read Preference can be set within connection URI or the Java / Spring Boot application.

If the default read preference is Primary, we don’t need to set the read preference specifically. do you have any other suggestions?

I suggest check your application code if you are setting at the reads you are doing (for this specific read functionality).

Further, I think you can tell more about your application and the code you are using for this functionality:

As per our architecture, the first request will fetch the document by using findbyid() method and ready to update content and save to db. After 200 or 300 milliseconds difference the second request will come and try to fetch the same document, that time I could see the document having old value. It will happen intermittently not all time. if am setting delay between the requests it never happen, without delay it will happen sometimes.

Do you have any suggestion to set read and write concern in connection url. As of now the application using below connection url
The connection url :
mongodb+srv://{USER}:{PASSWORD}@{HOST}/{DB_NAME}?retryWrites=true&w=majority

Hi @Rojan_John_V, Did you find a solution? I am facing the same issue.

Any updated on this issue. We are also facing same issue. I need some guide to fix/solution for this issue.

I’m having the same issue here, any news ?