Ssss
Ssss

Reputation: 152

Cosmos SDK returns empty results on a query without partition specified

.Net Cosmos SDK returns empty results on a query without partition specified. Works fine when partition key provided.

Did I miss something?

Code for iterating:

var iterator = this.cosmosLinqQuery.GetFeedIterator(queryable);

var results = new List<Entity>();

while (iterator.HasMoreResults)
{
     var result = await iterator.ReadNextAsync();

     results.AddRange(result.Resource);
}

return results;

Code for creating queryable:

var options = new QueryRequestOptions()
        {
            PartitionKey = PartitionKey.None,
            MaxItemCount = null,
            MaxConcurrency = -1,
            MaxBufferedItemCount = -1,
            EnableScanInQuery = true,
        };

IQueryable<Entity> queryable = container.GetItemLinqQueryable<Entity>(
            requestOptions: options,
            continuationToken: continuationToken,
            allowSynchronousQueryExecution: true);

Upvotes: 0

Views: 935

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136206

I believe the issue is with setting the PartitionKey value to PartitionKey.None in your query options here:

    var options = new QueryRequestOptions()
    {
        PartitionKey = PartitionKey.None,
        MaxItemCount = null,
        MaxConcurrency = -1,
        MaxBufferedItemCount = -1,
        EnableScanInQuery = true,
    };

Based on my understanding, you use PartitionKey.None to get the documents that are created with no partition key (it is entirely possible to create a document without partition key). From the documentation here:

The returned object represents a partition key value that allows creating and accessing documents without a value for partition key

Can you try by removing PartitionKey = PartitionKey.None from your query request options? Something like:

var options = new QueryRequestOptions()
{
    MaxItemCount = null,
    MaxConcurrency = -1,
    MaxBufferedItemCount = -1,
    EnableScanInQuery = true,
};

Then you should be able to fetch all documents in a container.

Upvotes: 2

Related Questions