简体   繁体   English

获取集合CosmosDb中的所有文档

[英]Get All documents in a Collection CosmosDb

I would like to return all documents in a collection for CosmosDb 我想返回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 谢谢

UriFactory.CreateDocumentUri creates a Uri for a document specific query. UriFactory.CreateDocumentUri为特定于文档的查询创建一个Uri。

What you want is all documents in a collection so what you need to create is to create a collection Uri. 您想要的是集合中的所有文档,因此您需要创建的是创建集合Uri。 You can do that by using UriFactory.CreateDocumentCollectionUri(DatabaseName, CollectionName); 您可以使用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 . 对于返回很多文档的操作,建议您使用.AsDocumentQuery()方法,然后在query.HasMoreResults执行ExecuteNextAsync

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. 您应该这样做,因为如果仅在IQueryable上使用.ToList() ,则CosmosDB SDK确实会通过电线同步跳闸,这将对应用程序的性能造成不利影响。

Here is an example from MSDN on how you can do that. 是MSDN上有关如何执行操作的示例。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM