-
-
Save rasmuschristensen/81d4e4694b4993765c3ad36d8f45fd36 to your computer and use it in GitHub Desktop.
public static class ContainerExtensions | |
{ | |
public static async Task<bool> ExistsAsync<T>(this Container container, Expression<Func<T,bool>> predicate) | |
{ | |
var qdef = container.GetItemLinqQueryable<T>().Where(predicate).ToQueryDefinition(); | |
var iterator = container.GetItemQueryIterator<T>(qdef.QueryText, null, new QueryRequestOptions { MaxItemCount = 1 }); | |
if (!iterator.HasMoreResults) return false; | |
var response = await iterator.ReadNextAsync(); | |
return response.FirstOrDefault() != null; | |
} | |
} |
Interesting I wasn't aware of that. what kind of scenarios would that be?
The easiest example I can think of is if you have a cross partition query where the container has 3 physical partitions in Cosmos DB. You say " select * from T where T.cost > 100"
. The first partition might have 0 items that match that query. So it will return a page with 0 items. In the next ReadNextAsync it will go to partition 2 and it also has 0 items. ReadNextAsync is called again and the 3rd partition return 10 items.
Okay I get what you're saying. If I specify a partitionkey it would work with out empty results right?
It's less likely but it's still possible with a PartitionKey. The one example with a partition key is "skip 100,000" items is likely to cause the first page to be empty. It is possible to get an empty page due to the backend preempting your query. This would mean that your query is taking more than some fixed amount of time on the backend to retrieve your documents, so it preempts your query and sends you a continuation if you want to continue making progress on the query.
Results are not guaranteed to be on the first page. There is several scenarios where the first page will be empty. You should drain all the results to verify that one of the following pages doesn't have a result.