Reputation: 520
I have a code in to get all data in one collection in cosmo db, if the collection is empty then start to inserting.
The first time the code is run collection is empty however, SetIterator.HasMoreResults
return True, even though the collection is empty.
Then there will be error raising Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: NotFound
, and I have checked with a collection that is not empty the code run fine.
I can use try catch to handle it, but it does not seems to be a nice solution, Does anyone know how to check if the collection is empty?
var itemList = new List<T>();
using (FeedIterator<T> setIterator = _container.GetItemLinqQueryable<T>()
.ToFeedIterator())
{
while (setIterator.HasMoreResults)
{
foreach (var item in await setIterator.ReadNextAsync())
{
itemList.Add(item);
}
}
}
return itemList;
Upvotes: 1
Views: 984
Reputation: 8384
You can use indexing to get empty collection in cosmos db. To do this you can use unique indexes. Unique indexes are created to only those which doesn’t contain any documents.
Important
Unique indexes can be created only when the collection is empty (contains no documents).
To create unique indexes on collections you can go through unique indexes
Upvotes: 1