gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:31:30 25 4
gpt4 key购买 nike

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

enter image description here


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

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"));

请提出一种实现此目的的方法。

最佳答案

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

以下是 MSDN 的摘录

If the collection the stored procedure is registered against is a single-partition collection, then the transaction is scoped to all the documents within the collection. If the collection is partitioned, then stored procedures are executed in the transaction scope of a single partition key.

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

关于azure - 从 CosmosDB 的集合中读取所有分区的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49071092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com