Reputation: 385
I have seen one similar query raised , but no proper solutions found.
sometimes, I am seeing duplicates records created for same matching conditions. Appreciate your help on finding solutions. MongoDB server version: 4.2.7 "mongoose": "^5.9.6"
function addModify() {
Sales.findOneAndUpdate(
{
shopId: shopId,
SalesId: SalesId
},
{
$set:
{
amount: amount
lastUpdated: new Date
}
}
, { upsert: true }, function (err, list) {
if (err) {
logger.error(' Sales-- error - ' + ' - ' + JSON.stringify(err));
cb({ err: 'failed transcation log' });
} else {
cb(null, null);
}
});
}
NB: Question Updated with more information based on comment received.
upsert is true is intentional , because the same routine is executed for addition and modification ( for matching shopId and salesId)
Just to clarify. This is not an always scenario. for experiment, I ran this in a loop for 10000 times. I could see, some times it insert more than 1 record( I could see upto 4 or 5) and NOT 10000 records
This code is accessed from an API end point with app server( nodejs) hosted in one EC2 instance and mongo on another EC2 instance
Upvotes: 1
Views: 462
Reputation: 43
You can set the { upsert: true }
property to false
and that way if a document is not found, a new one will not be inserted.
Upvotes: 1