【问题标题】:Azure cosmos db collection is not getting the partition keyAzure cosmos db 集合未获取分区键
【发布时间】:2019-01-15 19:27:11
【问题描述】:

我正在尝试在我的 c# 代码中创建一个 azure cosmos 数据库和集合。

await client.CreateDatabaseIfNotExistsAsync(new Database() { Id = "data"});
DocumentCollection dCollection = await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("data"), new DocumentCollection { Id = "coll"}, new RequestOptions { OfferThroughput = 400, , PartitionKey = new PartitionKey("/id") });
// dashboardCollection.PartitionKey.Paths.Add("/id");

当我转到portal.azure.com 并检查我的文档数据库时,该集合已创建。当我去Scale and Settings 收集时,我没有看到分区键。

我手动创建了另一个集合,它在Scale and Settings 部分中显示了分区键。

delete 函数由于此分区键错误而引发错误

已成功将 ID 为 1 的记录插入到文档数据库中。以下删除失败,表明 partitionKey 无效。

ResourceResponse<Document> response = await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri("data", "coll", "1"), new RequestOptions { PartitionKey = new PartitionKey("1") });

【问题讨论】:

    标签: c# azure azure-cosmosdb


    【解决方案1】:

    我来自 CosmosDB 工程团队。

    创建 DocumentCollection 时,请确保 DocumentCollection 对象中提供了分区键,如下所示:

    PartitionKeyDefinition pkDefn = new PartitionKeyDefinition() { Paths = new Collection<string>() { "/id" } };
    DocumentCollection dCollection = await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("data"), new DocumentCollection { Id = "coll", PartitionKey = pkDefn }, new RequestOptions { OfferThroughput = 400, PartitionKey = new PartitionKey("/id") });
    

    在收集 CRUD 请求期间,RequestOptions 上的 PartitionKey 不受支持,因为我们希望 PartitionKey 是收集对象的一部分。 RequestOptions 上的 PartitionKey 在文档 CRUD 请求期间得到遵守。

    【讨论】:

    • 几个与您的答案相关的问题。由于我们必须提供分区键 ID,Azure 门户是否按照您今天的描述创建了一个集合?您将如何在 ReadDocumentAsync 中引用分区键的值?我得到的只是具有指定 id 的实体不存在。
    猜你喜欢
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多