feedIterator.HasMoreResults is true but feedIterator.ReadNextAsync() is returning null

mohan b 41 Reputation points
2022-10-26T20:48:40.26+00:00

I have the following stub working when tested using azure cosmos db emulator but when tested by pointing to actual cosmos db I am seeing feedIterator.HasMoreResults is true and await feedIterator.ReadNextAsync() is not yeilding any results

var response = new RetrievePersonAddress();
var queryDefinition = new QueryDefinition("SELECT C.person.address FROM C JOIN T IN C.states WHERE T.zipCode = @ZipCode ").WithParameter("@ZipCode ", zipCode);
using (var feedIterator = _client.GetContainer(_dbname, _collectionName").GetItemQueryIterator<RetrievePersonAddress>(queryDefinition))
{
if (feedIterator.HasMoreResults)
{
var readResponse = await feedIterator.ReadNextAsync();
response = readResponse.FirstOrDefault();
}
}
Any inputs on what might be the problem?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,059 questions
0 comments No comments
{count} votes

Accepted answer
  1. Oury Ba-MSFT 11,376 Reputation points Microsoft Employee
    2022-10-28T00:25:22.613+00:00

    Hi @mohan b Thank you for posting your question on Microsoft Q&A and for using Azure services.

    As per my understanding, you are facing issue when testing command to Azure Cosmos DB you are seeing feedIterator.HasMoreResults is true and await feedIterator.ReadNextAsync() is not yielding any results. Please let me know if my understanding is not correct.

    You will need to drain the results in a while loop, empty pages can be returned while there are still more results. Example code with while loop: https://github.com/Azure/azure-cosmos-dotnet-v3/blob/8a3883e8321c4dcbd03e857ea2134438a882d990/Microsoft.Azure.Cosmos.Samples/Usage/Queries/Program.cs#L207

    Please let me know if that works

    Regards,
    Oury


1 additional answer

Sort by: Most helpful
  1. Oury Ba-MSFT 11,376 Reputation points Microsoft Employee
    2022-10-29T21:54:19.903+00:00

    Hi @mohan b Thank you for letting me know that draining the results in a while loop worked.

    Why are we getting empty pages? does the query that we run do not return point response?

    Doc on empty pages.
    https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/pagination#understanding-query-executions

    The query you have in your example has a single filter on zip code (a point read would be a query with filter on partition key and id of the document if you are trying to read a single document) here is a blog on the difference between point read and query: https://devblogs.microsoft.com/cosmosdb/point-reads-versus-queries/

    You can also rewrite your query to use EXISTS instead of JOIN since you aren't projecting the value you are filtering by:

    https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/subquery#exists-expression\\

    SELECT C.person.address

    FROM C

    WHERE EXISTS(

    SELECT VALUE T  
    
    FROM T IN C.states   
    
    WHERE T.zipCode = @zipCode  
    

    )

    Regards,
    Oury