c# - MongoDB c# 驱动程序使用 upsert 和 updateMany

标签 c# mongodb mongodb-csharp-2.0

在 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/

相关文章:

mongodb - 在 MongoDB 中查找共享一个键值的两个文档

c# - 旧 .net 驱动程序 MongoCollection.Save 的新替代方案?

c# - 我如何在托管 C++ 中执行 typeof(int)?

java - 如何使用 Java 在 Spark Rdd 中建立单个 Mongo 连接

c# - 资源中添加图片到Base64编码的ImageStream(resx)

mongodb - 蒙戈数据库 : Unable to create a sharding cluster in Ubuntu

c# - MongoDb 使用新的异步方法创建存储库模式

c# - 当我使用 ReplaceOneAsync 和 IsUpsert = true 时,mongodb 添加了一个空 ID。我该如何阻止呢?

c# - 可观察字典未按预期运行

c# - 在控制台应用程序中等待键盘事件