【发布时间】:2018-04-04 18:08:05
【问题描述】:
我很困惑 Azure Cosmos DB“UpsertDocumentAsync”C# API 的工作原理。 如果您第一次阅读该对象,则该对象似乎已更新:
var response = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, "docId"), new RequestOptions { PartitionKey = new PartitionKey("pk") });
var upsertOrder = response.Resource;
var upsertOrder = new Measurements { Id = "docId" , value = 3243};
upsertOrder.SetPropertyValue("value", 5678);
response = await client.UpsertDocumentAsync(collectionLink, upsertOrder);
如果我直接创建一个对象:
var upsertOrder = new Measurements { Id = "docId" , value = 3243};
response = await client.UpsertDocumentAsync(collectionLink, upsertOrder);
这会创建一个新对象!!在修补之前我真的必须阅读文档吗??
编辑 我知道我也需要添加 partitionKey。因此它不再创建新对象,而是将所有未传递的字段设置为 NULL。这不是补丁行为!那么我是否正确我需要传递所有字段?
非常感谢在这里遮光。
【问题讨论】:
-
@BernardVanderBeken 谢谢。我知道我也需要添加 partitionKey。因此它不再创建新对象,而是将所有未传递的字段设置为 NULL。这不是补丁行为!那么我是否正确我需要传递所有字段?
-
微软提供的 DocumentDB sdk 处理 POCO 对象和 crud 操作的方式有点奇怪。我建议使用Cosmonaut 来处理此类复制问题。免责声明:我是 Cosmonaut 的作者
-
@Elfocrash 谢谢。那么,您确认用于 upsert 的 CosmosDB API 不支持真正的补丁行为?
-
可以,但您必须将 Measurements 类
Id更改为小写id以便它自动映射到 cosmosdb api 正在创建的文档或使用[JsonProperty("id")]属性Id 属性。它有很多这样的小东西,这就是我推荐这个包的原因。
标签: azure azure-cosmosdb upsert