Search code examples
mongodbconcurrencymongodb-javaconcurrentmodification

Concurrent partial updates in Mongo collection


Consider the following mongo document

{
  _id:...
  param1:oldValue1
  param2:oldValue2 
}

Suppose if am trying to do two concurrent partial updates with the following queries:

db.collection.update(  { _id:...} , { $set: { param1 : "newValue1"  } }
db.collection.update(  { _id:...} , { $set: { param2 : "newValue2"  } }

Will I get the following docuemnt state in mongo after these concurrent partial updates:

{
  _id:...
  param1:newValue1
  param2:newValue2
}

Does two concurrent updates leave the document with updated values considering the fact that the concurrent updates dont have common fields.without concurrent modification issue?


Solution

  • Yes, regardless of the execution order of the two updates, the doc will end up as you show it. This is because the two atomic $set operations target distinct fields, and any field not referenced in the update isn't modified.