I am trying to increase post’s comment vote in atomic operation so I am trying to upvote post’s comment if current username is not exist in DownVoters and UpVoters list of comment.But below query really strangely sometimes works but not always. I have checked that username is not exist in DownVoters and UpVoters list but still below query does not work. What is wrong at below query ?
var query = Query.And(Query.EQ("Comments._id", commentId), Query.NotIn("Comments.DownVoters", username));
var update = Update.Push("Commments.$.UpVoters",username).Inc("Commments.$.Score", 1);
collection.Update(query, update);
Your code looks fine. I’ve wrote it many times and it work as expected. It can be simple human mistake in any field name (like you have above Commments -> Comments).
I suggest to play in mongodb shell to figure out exact problem.
First of all insert test document:
Also I’ve translated your c# query to native mongodb syntax, so you can use it in mongodb shell update.
Your query:
Your update:
Try it out and come back to us with answer what was a problem.