MongoDB $max operator not working properly on updation

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)
                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);
                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 ?

Hi @Aneesh_S,

What do you mean by “run concurrently”? Are you running multiple client instances that are sending the same update command? If so, why?


Your filter only specify docID, so if you call UpdateTotal many times you will update many times the same document. Your code does not respect your requirement

You must add the value is higher than the old value in your Filter.