CosmosDB分页返回值

时间:2018-07-02 23:07:44

标签: rest azure asp.net-core azure-cosmosdb paging

我正在尝试从CosmosDB返回分页结果请求。我从here看了这个例子,但不确定如何使用response变量。

// Fetch query results 10 at a time.
var queryable = client.CreateDocumentQuery<Book>(collectionLink, new FeedOptions { MaxItemCount = 10 });
while (queryable.HasResults)
{
    FeedResponse<Book> response = await queryable.ExecuteNext<Book>();
}

我想直接退回吗?还是我必须对response变量做进一步的事情?我试图直接返回响应变量,但它不起作用。这是我的代码:

 public async Task<IEnumerable<T>> RunQueryAsync(string queryString)
        {

            var feedOptions = new FeedOptions { MaxItemCount = 3 };
            IQueryable<T> filter = _client.CreateDocumentQuery<T>(_collectionUri, queryString, feedOptions);
            IDocumentQuery<T> query = filter.AsDocumentQuery();
            var response = new FeedResponse<T>();
            while (query.HasMoreResults)
            {
                response = await query.ExecuteNextAsync<T>();
            }
            return response;
        }

更新: 阅读@Evandro Paula的答案后,我遵循了URL,并将实现方式更改为以下内容。但这仍然给我500状态代码:

 public async Task<IEnumerable<T>> RunQueryAsync(string queryString)
        {
            var feedOptions = new FeedOptions { MaxItemCount = 1 };
            IQueryable<T> filter = _client.CreateDocumentQuery<T>(_collectionUri, queryString, feedOptions);
            IDocumentQuery<T> query = filter.AsDocumentQuery();
            List<T> results = new List<T>();
            while (query.HasMoreResults)
            {
                foreach (T t in await query.ExecuteNextAsync())
                {
                    results.Add(t);
                }
            }
            return results;
        }

这是异常消息:

  

跨分区查询是必需的,但已禁用。请设定   将x-ms-document-db-query-enablecrosspartition设置为true,指定   x-ms-documentdb-partitionkey,或修改查询以避免这种情况   Windows / 10.0.17134 documentdb-netcore-sdk / 1.9.1

更新2 : 我将EnableCrossPartitionQuery添加为true,并且能够从CosmosDB获得响应。但是我无法获得我定义的1个项目。相反,我得到了11个物品。

2 个答案:

答案 0 :(得分:3)

在下面找到一个有关如何使用CosmosDB / SQL分页查询的简单示例:

    private static async Task Query()
    {
        Uri uri = new Uri("https://{CosmosDB/SQL Account Name}.documents.azure.com:443/");
        DocumentClient documentClient = new DocumentClient(uri, "{CosmosDB/SQL Account Key}");

        int currentPageNumber = 1;
        int documentNumber = 1;

        IDocumentQuery<Book> query = documentClient.CreateDocumentQuery<Book>("dbs/{CosmoDB/SQL Database Name}/colls/{CosmoDB/SQL Collection Name}", new FeedOptions { MaxItemCount = 10 }).AsDocumentQuery();

        while (query.HasMoreResults)
        {
            Console.WriteLine($"----- PAGE {currentPageNumber} -----");

            foreach (Book book in await query.ExecuteNextAsync())
            {
                Console.WriteLine($"[{documentNumber}] {book.Id}");
                documentNumber++;
            }

            currentPageNumber++;
        }
    }

您问题中描述的每个异常跨分区查询是必需的但已禁用,请按以下方式更新供稿选项:

var feedOptions = new FeedOptions { MaxItemCount = 1, EnableCrossPartitionQuery = true};

https://github.com/Azure/azure-documentdb-dotnet/blob/d17c0ca5be739a359d105cf4112443f65ca2cb72/samples/code-samples/Queries/Program.cs#L554-L576上找到更全面的示例。

答案 1 :(得分:1)

您没有为特定项目指定任何条件...因此您将获得所有结果。.尝试为您要寻找的项目(id,名称等)指定条件。并且请记住,跨分区查询消耗的时间要多得多,您可以重新访问数据模型的体系结构。理想情况下,始终在同一分区中进行查询