Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb C#在子集合上批量更新/替换_C#_Mongodb_Mongodb .net Driver_Bulk Operations - Fatal编程技术网

Mongodb C#在子集合上批量更新/替换

Mongodb C#在子集合上批量更新/替换,c#,mongodb,mongodb-.net-driver,bulk-operations,C#,Mongodb,Mongodb .net Driver,Bulk Operations,给出了以下对象结构: public class RootDocument { public Guid Id { get; set; } public string SomeProperty { get; set; } public List<ChildDocument> Documents { get; set; } } public class ChildDocument { public Guid Id { get; set; } pub

给出了以下对象结构:

public class RootDocument
{
    public Guid Id { get; set; }
    public string SomeProperty { get; set; }
    public List<ChildDocument> Documents { get; set; }
}

public class ChildDocument
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string SomeProperty { get; set; }
}
但是
AddToSet
不是现有
ChildDocument
的替换操作

使用最新的MongoDB C#驱动程序实现此要求的最佳方式是什么?

请阅读。在这种情况下,您不需要批量,只需要更新即可

collection.UpdateMany(
  Builders<RootDocument>.Filter.Eq("Documents.Id", document.Id),
  Builders<RootDocument>.Update.Set("Documents.$", document));
collection.updateName(
Builders.Filter.Eq(“Documents.Id”,document.Id),
Builders.Update.Set(“Documents.$”,document));

这将遍历集合,并将具有指定Id的ChildDocument的任何RootDocument匹配,然后将其替换为提供的文档。

非常感谢
collection.UpdateMany(
  Builders<RootDocument>.Filter.Eq("Documents.Id", document.Id),
  Builders<RootDocument>.Update.Set("Documents.$", document));