简体   繁体   中英

Mongodb C# driver - Update document then select collection with updated document

I'm updating a document in Mongodb with the C# driver. I've verified the update is successfully completing, but if I select the collection that contains the updated document immediately after the update I'm not seeing the new values immediately. If I put a breakpoint in my code after the update but before the select, I will see the new values in the results from the Select. If I let the code run straight through, I get the old values in my names collection. I tried changing the write concern, but I don't think that's it. Is there some way to chain these two operations together so the select won't happen until the update has completed?

var qry = Query.EQ("_id", new ObjectId(id));
var upd = Update.Set("age", BsonValue.Create(newAge));

db.GetCollection<MongoTest>("mongotest").Update(qry,upd);

... would like to pause here until update is complete ...    

var names = db.GetCollection<MongoTest>("mongotest")
     .FindAll().SetSortOrder(SortBy.Ascending("name"))
     .ToList<MongoTest>();

if (names.Count() > 0)
{
     return View(names);
}

One clarification. The official MongoDB .Net driver defaults to Acknowledged write (write concern 1) when you start with using MongoClient. If you start with the old style of using MongoServer.Create (now obsoleted), the default is Unacknowledged. Also ensure that you are not using a read preference that could route your reads to a secondary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM