Get All documents in a Collection CosmosDb

五迷三道 提交于 2019-12-11 00:37:05

问题


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


回答1:


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
        }
    }
}


来源:https://stackoverflow.com/questions/51191516/get-all-documents-in-a-collection-cosmosdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!