I am using MongoDB $max operator to update total of selected document if the value is higher than the old value. like below,
public bool UpdateTotal( string docid,long newtotal)
{
try
{
var collection= db.Collection<MyCollection>("MyCollection");
var filter = Builders<MyCollection>.Filter.Eq("docID", docid);
var updatedata = Builders<MyCollection>.Update.Max(x=>x.Total, newtotal);
var upoptions = new UpdateOptions();
var res = collection.UpdateOneAsync(filter,updatedata);
res.Wait();
var issuccess = res.Result.IsAcknowledged;
Debug.WriteLine("MongoDBUpdate " + newtotal+ " " +res.Result.ModifiedCount);
return issuccess;
}
catch (Exception exception)
{
}
}
The Query is updating the field in MongoDB. But the problem is when it is run concurrently the update is happening more than once. The debugger is showing modified count=1 in more than one request. why is this happening ?, how can I stop this from happening in this situation.
can someone tell help me see what am I doing wrong here ?