从CosmosDB中读取集合中所有分区的文档

问题描述 投票:2回答:1

使用电子邮件作为分区键在Cosmos DB中创建了一个集合,并在该集合中添加了大约十个文档。

enter image description here

当我在Partition Key中使用RequestOptions查询集合时,返回所有具有提供的分区键的文档。

var result = await documentRUClient.ExecuteStoredProcedureAsync<string>
(UriFactory.CreateStoredProcedureUri(DataBaseId, CollectionId, 
"GetCollection"), new RequestOptions { PartitionKey = 
new PartitionKey("test @test.com") });

存储过程:

function GetCollection() {
    var context = getContext();
    var collectionManager = context.getCollection();        
    var collectionLink = collectionManager.getSelfLink();

    var query = "SELECT * FROM c";

    collectionManager.queryDocuments(collectionLink, query, function(err, documents){
        context.getResponse().setBody(documents);
    });
}

现在我需要从该集合中的所有分区获取所有文档。我已经尝试了下面的代码,但是它给出了错误,必须为此操作提供PartitionKey值。

 var result = await documentRUClient.ExecuteStoredProcedureAsync<string>
    (UriFactory.CreateStoredProcedureUri(DataBaseId, CollectionId, 
    "GetCollection"));

请建议一种方法来实现这一目标。

azure stored-procedures azure-cosmosdb
1个回答
2
投票

这是Cosmos中存储过程的限制。它只能在分区内执行。

以下是MSDN的摘录

如果注册存储过程的集合是单分区集合,则事务的范围限定为集合中的所有文档。如果集合是分区的,则存储过程在单个分区键的事务范围内执行。

您必须使用CreateDocumentQuery并在FeedOptions中将EnableCrossPartitionQuery设置为true才能获取集合中的所有文档。

© www.soinside.com 2019 - 2024. All rights reserved.