MongoDB – multi wirte data to same document

mongodb

I have a question, and its about how mongo will handle if 2 user at same time make a update request to update the data with the $set function like this

Update reuqest 1:

{
   $set: {
      'field1' : 'new data'
   }
}

Update request 2:

{
   $set: {
      'field2' : 'secound data'
   }
}

if i using mysql, its will update each field without eny issue, will mongodb do the same?

the next case can be i have 3 users and there will be a update like this

Update request 1:

{
   $set: {
      'field1' : 'data'
   }
}

Update request 2:

{
   $set: {
      'field1' : 'data',
      'field2' : 'data'
   }
}

Update request 3:

{
   $set: {
      'field2' : 'data'
   }
}

in this case if i do the same in MySQL its will request 2 overwirte the field1 from request 1 and the request 3 will overwirte the field2 data in request 2.

and i expect mongo work at the same way? or can i make a version control over a document so i can se changes over time?

i need to prevent this case, becures i'm building a huges API layer where evey thing use Mongo and i want to update product catalog on cross from platform to platform.

hope eny how can help me to understand this part in MongoDB

Best Answer

With (multiple) updates to (single) document, first come, first served. So, that client who is first (in time) get lock to that document, others who come later, will get "ticket" (queue number) and will wait their turn to update that document.

So, update 2 will override field1 value and add field2, update 3 then will override field2 value..

Related Question