How to get more than 100 query results with Azure DocumentDB REST API

陌路散爱 提交于 2021-01-28 07:31:55

问题


I am following a sample for Azure DocumentDB below. In the sample, C# code queries for documents in the DocumentDB.

https://github.com/Azure/azure-documentdb-dotnet/blob/master/samples/rest-from-.net/Program.cs

Line 182:

var qry = new SqlQuerySpec { query = "SELECT * FROM root" }; 
var r = client.PostWithNoCharSetAsync(new Uri(baseUri, resourceLink), qry).Result; 

The problem is the result 'r' only contains the first 100 documents. If I use the Client SDK, I can get more than 100. I tried using stream, but had no luck so far. Any help would be appreciated!


回答1:


For a SQL query the results are returned in segments if the result set is too large. The results are returned in chunks of 100 items or 1 MB (whichever limit is hit first) by default.

You can either use continuation tokens to get each segment after another. Or you set the x-ms-max-item-count custom header in a request to increase the limit to an appropriate value.

You can have a look the the REST API for further details.

For the sample program you have to add the line

client.DefaultRequestHeaders.Add("x-ms-max-item-count", "1000");

in order to get 1000 documents instead of 100.




回答2:


I'm just guessing here, but it might be worth a shot. Here's the documentation from MSDN that describes the List action:

https://docs.microsoft.com/en-us/rest/api/documentdb/list-documents

In the "Headers" section under "Response" it is mentioned that you might get an optional token in the header "x-ms-continuation". Based on the description you have to issue another GET request with this token specified to get the other elements of the result set.

Can you check whether you get a header like this in the response? If so, you can issue another get request with this token specified (see the same documentation page under "Request").



来源:https://stackoverflow.com/questions/42600008/how-to-get-more-than-100-query-results-with-azure-documentdb-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!