Reputation: 2098
I would like to return all documents in a collection for CosmosDb
My code is as follows
client.CreateDocumentQuery(UriFactory.CreateDocumentUri(dbName, colName,"id")).ToList();
It is not working. I can find a specific document but not all
Thanks
Upvotes: 4
Views: 10886
Reputation: 7200
UriFactory.CreateDocumentUri
creates a Uri for a document specific query.
What you want is all documents in a collection so what you need to create is to create a collection Uri.
You can do that by using UriFactory.CreateDocumentCollectionUri(DatabaseName, CollectionName);
Just a note on your practice however.
For operations that return a lot of documents you are recommended to use the .AsDocumentQuery()
method and then do ExecuteNextAsync
while query.HasMoreResults
.
You should do it that way because the CosmosDB SDK does make trips over the wire synchronously if you just use .ToList()
on the IQueryable
and this will be bad for your application's performance.
Here is an example from MSDN on how you can do that.
using (var queryable = client.CreateDocumentQuery<Book>(
collectionLink,
new FeedOptions { MaxItemCount = 10 })
.Where(b => b.Title == "War and Peace")
.AsDocumentQuery())
{
while (queryable.HasMoreResults)
{
foreach(Book b in await queryable.ExecuteNextAsync<Book>())
{
// Iterate through books
}
}
}
Upvotes: 11