How to use $push update modifier in MongoDB and C#, when updating an array in a document

前端 未结 3 1492
独厮守ぢ
独厮守ぢ 2021-01-01 20:56

I\'ve run the following code in mongo shell:

db.unicorns.insert({name:  \'Dunx\',  loves:  [\'grape\',  \'watermelon\']});

and now I\'ve so

3条回答
  •  情话喂你
    2021-01-01 21:38

    To do this with the updated syntax and regular BsonDocuments instead of defined objects, use the following:

    var filter = Builders.Filter.Eq("name": "Aurora");
    var update = Builders.Update.Push("loves", "sugar"):
    
    // you can also use the async update method from Alex's answer here
    var result = fantasyContext.Unicorns.UpdateOne(filter, update);
    

提交回复
热议问题