在 MongoDB c# 驱动程序 (2.0+) 中,我们可以在执行和 updateManyAsync 时执行更新插入吗? This示例有助于 UpdateOne,但我正在寻找适用于 updateMany 的东西。
最佳答案
UpdateDefinition<Phone> updateDefinition = Builders<Phone>.Update.Set(x => x.Name, "Updated Name");
MongoDb.GetCollection<Phone>("Phone").UpdateOne(x => x._id == "foo", updateDefinition); // replaces first match
MongoDb.GetCollection<Phone>("Phone").UpdateMany(x => x.SomeProp == 5, updateDefinition); // replaces all matches
假设你有一个类:
class Phone
{
public string _id {get; set;} // this is the primary key because Mongo uses _id as primary key
public string Name {get;set;}
public int SomeProp {get;set;}
// etc...
}
关于c# - MongoDB c# 驱动程序使用 upsert 和 updateMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33966073/