Reputation: 789
I have encountered strange behavoir while testing my application. I have a request to update multiple documents and increment one of thier fields by 1 (and also decrease other by -1, but dont think that this is the point).
Query = { "_id" : { "$in" : [ObjectId("4fbf8481a975b5237c56f221"),
ObjectId("4fbf8481a975b5237c56f222"),
ObjectId("4fbf8481a975b5237c56f223")] } };
Update = { "$inc" : { "PartlyCompleted" : -1, "Completed" : 1 } }
The first document updates OK, but two others are not affected.
What could be the reason. Please give me advise where to look.
Some aditional information. I'm using C# official driver. Here is some of the code:
_personsId = new BsonArray();
_personsId.AddRange(personsId.Where(x => DataService.IsIdValid(x)).
Select(x => new ObjectId(x)).ToArray());
var query = Query.In("_id", _personsId);
var update = Update.Combine(
Update.Inc("Completed", 1 ),
Update.Inc("PartlyCompleted", -1));
DataService.PersonSet.Collection.Update(query, update);
I also checked the number of documents returned by the query below, and it is 3:
var count = DataService.PersonSet.Collection.Count(query);
Upvotes: 0
Views: 488
Reputation: 230326
By default updates affect only the first matched document.
So, in your example mongo picks one of those 3 documents and increments counters. To update multiple documents you need to set the UpdateFlags.Multi
flag.
I am curious, do you use this flag in other updates? If yes, why the question? If no, are you sure those updates touch multiple documents at once?
Upvotes: 3