【问题标题】:Read before Upsert in Azure Cosmos DB在 Azure Cosmos DB 中进行 Upsert 之前阅读
【发布时间】: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


【解决方案1】:

https://feedback.azure.com/forums/263030-azure-cosmos-db/suggestions/6693091-be-able-to-do-partial-updates-on-document

如您所见,该功能已在计划中,但仅在 2018 年 3 月 5 日收到反馈,因此可能需要一段时间才能交付。

目前您可以读取文档,更新它,然后按照您的说明插入 - 文档大小是重要设计选择的部分原因,或者您也可以编写一个存储过程来执行此操作并使用它部分更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    相关资源
    最近更新 更多