findAndModify() method - Query and Update document atomically in MongoDB

You are here : Home / MongoDB Tutorial

Contents of page >

0) What is findAndModify() method in MongoDB?
findAndModify() method atomically update the document in MongoDB.

1) How to use findAndModify() method in MongoDB >

STEP 1.1) create and insert in TRAIN collection >
TRAIN collection contains passengerBookingDetails as embedded documents.
db.TRAIN.insert({
 _id : 12,
 numberOfTicketsAvailable : 1,
 PassengerBookingDetail : [
   { passengerId : 21},
   { passengerId : 22}
 ]
})

STEP 1.2) Now, let’s use findAndModify() method to find and update the document atomically.
  • query  
    • Below query will check if _id =12 and numberOfTicketsAvailable >= 1
  • sort
    • sort by numberOfTicketsAvailable.
  • update
    • numberOfTicketsAvailable will be incremented by -1 (i.e. decremented by 1) and
    • PassengerBookingDetail will be inserted in collection.
Output will show original (i.e. just before modification) document selected for this update.
db.TRAIN.findAndModify({
  query : { _id : 12, numberOfTicketsAvailable:{$gt:0}},  
  sort : { numberOfTicketsAvailable : 1 },  
  update : {
     $inc:{numberOfTicketsAvailable:-1},
     $push:{PassengerBookingDetail:{passengerId : 30} }
  }    
})
Output>
{
       "_id" : 12,
       "numberOfTicketsAvailable" : 1,
       "PassengerBookingDetail" : [
               {
                       "passengerId" : 21
               },
               {
                       "passengerId" : 22
               }
       ]
}
Output showed original (i.e. just before modification) document selected for this update.

STEP 1.3) Now, let’s query TRAIN document after findAndModify() method >
db.TRAIN.find()
Output>
{
       "_id" : 12,
       "numberOfTicketsAvailable" : 0,
       "PassengerBookingDetail" : [
               {
                       "passengerId" : 21
               },
               {
                       "passengerId" : 22
               },
               {
                       "passengerId" : 30
               }
       ]
}

2) What problems can happen in absence of use atomicity when 2 passengers simultaneously tries to book ticket?
Let’s say TRAIN and passengerBookingDetails are in different collections.
And passenger1 and passenger2 tries to book ticket at same time.
First, passenger1 tried to book ticket and saw that numberOfTicketsAvailable is 1 in TRAIN collection then update passengerBookingDetails, and after update it will deceremnt numberOfTicketsAvailable by 1. But, it was in mid of updating passengerBookingDetails and meanwhile passenger2 tried to book ticket and saw that numberOfTicketsAvailable is still 1 in TRAIN collection then update passengerBookingDetails. So, there will be discrepancy in data. As, entry for both passengers will go there in passengerBookingDetails and numberOfTicketsAvailable in TRAIN collection will be -1.
So, we can solve above problem by using findAndModify() method.

3) Summary -
So in this MongoDB tutorial we learned how to use findAndModify() method to find and update the document atomically.
db.TRAIN.findAndModify({
  query : { _id : 12, numberOfTicketsAvailable:{$gt:0}},  
  sort : { numberOfTicketsAvailable : 1 },  
  update : {
     $inc:{numberOfTicketsAvailable:-1},
     $push:{PassengerBookingDetail:{passengerId : 30} }
  }    
})

Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.

RELATED LINKS>

What is MongoDB - A quick introduction to database


Labels: MongoDB
eEdit
Must read for you :