给出了以下对象结构:
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; }
}
我想更新集合中所有ChildDocuments
指定Id
的所有RootDocument
内容。RootDocument
我们的想法是像这样在批量操作中更新它们:
var document = new ChildDocument() { Id = <id of existing ChildDocument>, Name = ..., ...);
var bulkOperations = new WriteModel<RootDocument>[]
{
new UpdateManyModel<RootDocument>(
Builders<RootDocument>.Filter.Eq("Documents.Id", document.id),
Builders<RootDocument>.Update.AddToSet(x => x.Documents, document))
};
await mongoDatabase.GetCollection<RootDocument>()
.BulkWriteAsync(bulkOperations, new BulkWriteOptions { IsOrdered = false });
但AddToSet
不是现有的替换操作ChildDocument
。
最新的 MongoDB C# 驱动程序实现此要求的最佳方式是什么?