3

For some reason I cannot get paging properly working for Azure DocumentDB

Here is a code snippet

var finalResults = new List<MyResult>();
// Here I am setting max page size to be 10
var options = new FeedOptions { MaxItemCount = 10 };
var query = client.CreateDocumentQuery<MyResultItem>(collection.SelfLink, options)
                  .SelectMany(r => r.Results)
                  .Where(r => r.Result > 75)
                  .Select(r => r)
                  .AsDocumentQuery();
 // it only works 1 iteration and next iteration I always get HasMoreResults = false
 // if I set MaxItemCount = 20, then I get 20 only
 while (query.HasMoreResults)
 {
     var pagedResults = query.ExecuteNextAsync<MyResult>().Result;
     foreach (var pagedResult in pagedResults)
     {
         finalResults.Add(pagedResult);
     }
 }

No matter what value I set for MaxItemCount, it will only get that many items and will not get the next batch, so I get query.HasMoreResults always returning false on the second iteration. I cannot find where the problem is

Update:

Json structure is as follows:

.NET object name MyResultItem

{
    Id: "xxxxxxxxxxxxxxxxxxxxx",
    Name: "xxxxxxxxxxxxxxx",
    Results: [
        { Id: 1, Result: 123 },
        { Id: 2, Result: 40 },
        { Id: 3, Result: 75 }
    ]
}

I am trying to get a flat list of "Results" where Result > 75

2
  • For clarification - how many results are you expecting? From the question I assume you're expecting than 20 documents? Mind posting some sample data?
    – Andrew Liu
    Commented May 11, 2015 at 17:52
  • @aliuy Hi, I am expecting about 975 results. I think there is a limitation on SelectMany. Looks like you can only "page" top level documents, not children of the object (i.e. r.Results is a child array within MyResults). Although, I may be wrong
    – fenix2222
    Commented May 11, 2015 at 23:22

1 Answer 1

1

This appears to be a bug w/ DocumentDB; the continuation token misbehaves for selectmany (and joins) when the number of elements in the child array is less than the request's page size.

I found a similar thread on the MSDN forums on this subject.

A fix is currently being worked on.

In the meantime, you can try to work around this issue by increasing the page size (e.g. set it to the max of 1,000).

4
  • Good to know. Setting it to 1,000 defeats the purpose of having paging in the first place. What if I have 5000 items, can I set it to 1000 and still get 5 pages?
    – fenix2222
    Commented May 12, 2015 at 0:24
  • Also, I don't get this limitation "Maximum number of values per IN expression* 100", if I set MaxItemCount = 1000, I do get 1000, not 100.
    – fenix2222
    Commented May 12, 2015 at 0:45
  • Right, you can set the page size to max (1000) as a temporary workaround; this would not make sense after the bug is fixed. For 5000 items - the query will work depending on the shape of the document. For example the query should always work for a data sent of 5000 docs, however you may get incorrect results for a data set of 2 documents with 2500 array elements each (array size > page size). Selectmany flattens elements from the array. If the IN operator is used on the parent document, you can expect expanded results.
    – Andrew Liu
    Commented May 12, 2015 at 2:33
  • You can output the toString() of the LINQ query to see the equivalent SQL syntax. This may help shed some light on the query behavior.
    – Andrew Liu
    Commented May 12, 2015 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.