Issue
This Content is from Stack Overflow. Question asked by manoj sahni
I have to send some objects data form angular reactive form to MongoDB, in that object some value will be changed and some unchanged, i use like below code
db.findByIdAndUpdate({_id:id},{item1:value1,item2:value2,item3:value3})
if any value ie value1 or value2 or value3 is changing then update operation will be ok but nothing any change then how can skip this updation,i want do this because i want avoid unnecessarily server interaction
Solution
tl;dr–You can’t skip the interaction with the DB.
Basically, you only want to update your record in the database when the user changes one of its values on the UI. This means you have two different versions of the same record: one in the UI, and one in the DB. If you’re sending the one from the UI to the DB, you basically have two options:
- save the version from the UI, no matter what is in the DB
- retrieve the value from the database and compare; save the version from the UI if they are different
You might’ve noticed that the first option has fewer interactions with the DB on average. This is good if you have high latency between your server and your DB.
But, on the other hand, the second option has fewer writes than the first option. If concurrent writes to the same record are common, and they’re causing timeouts, then this might be the best option for you.
This Question was asked in StackOverflow by manoj sahni and Answered by danieldaugherty It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.