azure-cosmosdb - 文档计数查询忽略 PartitionKey

标签 azure-cosmosdb

我希望获得所选分区中所有文档的数量。但是,以下代码将返回集合中所有文档的计数并且成本为 0 RU。

    var collectionLink = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);

    string command = "SELECT VALUE COUNT(1) FROM Collection c";

    FeedOptions feedOptions = new FeedOptions()
    {
        PartitionKey = new PartitionKey(BuildPartitionKey(contextName, domainName)),
        EnableCrossPartitionQuery = false
    };

    var count = client.CreateDocumentQuery<int>(collectionLink, command, feedOptions)
        .ToList()
        .First();

添加 WHERE c.partition = 'blah'查询的子句将起作用,但在集合中有 11 个文档时花费 3.71 RU。

为什么上面的代码片段会返回整个集合的计数,是否有更好的解决方案来获取所选分区中所有文档的计数?

最佳答案

If the query includes a filter against the partition key, like SELECT * FROM c WHERE c.city = "Seattle", it is routed to a single partition. If the query does not have a filter on partition key, then it is executed in all partitions, and results are merged client side.



您可以从这个官方 doc 中检查 SDK 执行的逻辑步骤。当我们向 Azure Cosmos DB 发出查询时。

If the query is an aggregation like COUNT, the counts from individual partitions are summed to produce the overall count.



所以当你只使用 SELECT VALUE COUNT(1) FROM Collection c ,它在所有分区中执行,结果合并到客户端。

如果您想获取所选分区中所有文档的数量,只需添加 where c.partition = 'XX'筛选。

希望对你有帮助。

关于azure-cosmosdb - 文档计数查询忽略 PartitionKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49250113/

相关文章:

azure - 在 DocumentDb Select 中形状不正确

CosmosDB 更改源上的 Azure V1 功能会在发布时触发所有更改

c# - 查询 DocumentDB 中的分区列表(和/或分区键值)

azure - 当 HasMoreResults 为 True 时,为什么 CosmosDB FeedResponse 不包含结果

javascript - 存储过程 azure Cosmos DB 返回空集合

c# - 文档 DbChangeFeed 中的 MaxItemCount

Azure Cosmos DB 存储过程不返回文档

azure - 删除 CosmosDB 中的 'foreign key' 关系?

ravendb - Azure DocumentDb 与 RavenDb

c# - DocumentDb 无法处理列名中的连字符 (-)