10 ตุลาคม 2565

MongoDB SELECT ... FOR UPDATE inside Transactions

 In MongoDB, if you want to lock document in the transaction, you need to update it.

The sample code below is to emulate SELECT FOR UPDATE in MongoDB, we need additional field "myLock" and we will utilize ObjectID function to make sure that it is unique.

var doc = db.foo.findOneAndUpdate(

    { _id: 1 }, 

    { $set: { myLock: { appName: "myApp", pseudoRandom: ObjectId() } } }

)


reference: How To SELECT ... FOR UPDATE inside MongoDB Transactions | MongoDB

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

บทความยอดนิยม (ล่าสุด)

บทความยอดนิยม (All Time)