Upserting in Mongo DB using official C# driver

后端 未结 4 743
别跟我提以往
别跟我提以往 2020-12-04 18:04

In the official documentation of mongodb they mention upserts, so it would be really nice to write an upsert command instead of:

if (_campaignRepo.Exists(cam         


        
4条回答
  •  一生所求
    2020-12-04 18:14

    You can use the regular update command, but just pass it the Upsert update flag

    MongoCollection collection = db.GetCollection("matches");
    var query = new QueryDocument("recordId", recordId);
    
    var update = Update.Set("FirstName", "John").Set("LastName","Doe");
    matchCollection.Update(query, update, UpdateFlags.Upsert, SafeMode.False);
    

    That code is adapted from a working application (shortened for clarity)

提交回复
热议问题