c# - Mongo update response says no document updated, but the document is there -
i'm using mongodb 2.6.10 , using c# driver 1.9.2. server has replicaset of 2 servers.
my documents of format. itemid unique.
{ "itemid": 2314, "favorites": [1, 24, 26, 34] }
then have code remove favorite of form
var query = query.eq("itemid", itemid); var result = collection.update(query, update.pull("favorites", favoriteidtoremove));
after each time, check result.documentsaffected equal 1. once in while, value comes 0. when go mongodb myself, can find document matching itemid, , can see favoriteid tried remove in array still there. result.ok true, , there's no error information.
what cause fail?
i'm no expert guess write concerns since there different levels of guarantee writing , updating documents in mongodb. see write concerns
so instead of using method:
mongocollection.update method (imongoquery, imongoupdate)
it might better use method instead:
mongocollection.update method (imongoquery, imongoupdate, writeconcern)
and specify writeconcern be
writeconcern.wmajority
that way update has highest guarantee.
Comments
Post a Comment