Let me be real simple. I am running node.js server. When I receive data from patch request, first I need to check in database. If the row exists I will update it, otherwise I will create that record. Here is my code. This is what I am calling in the request handler callback.
let dbCollection = db$master.collection('users'); createUser(req.body).then(user => { dbCollection.updateMany( { rmisId: req.body.rmisId }, { $set: { ...user } }, { upsert: true } ).then((result) => { log.debug("RESULTS", user); return result; }) .catch((err => { log.debug(err); return err; })); }) This is working fine in sequential requests. But its creating duplicate record when I receive 10 concurrent request. I am running on my local machine and replicating concurrent request using Apache JMeter. Please help me if you have experienced this kind of problem.
Thank you !
UPDATE
I have tested another approach that reads the database like dbCollection.find({rmisId: req.body.rmisId}) the database for determine its existing or no. But it has no difference at all.