获取集合CosmosDb中的所有文档 [英] Get All documents in a Collection CosmosDb

查看:82
本文介绍了获取集合CosmosDb中的所有文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想返回CosmosDb集合中的所有文档

I would like to return all documents in a collection for CosmosDb

我的代码如下

 client.CreateDocumentQuery(UriFactory.CreateDocumentUri(dbName, colName,"id")).ToList();

它不起作用.我可以找到一个特定的文档,但不是全部

It is not working. I can find a specific document but not all

谢谢

推荐答案

UriFactory.CreateDocumentUri为文档特定查询创建Uri.

UriFactory.CreateDocumentUri creates a Uri for a document specific query.

您想要的是集合中的所有文档,因此您需要创建的是创建一个集合Uri. 您可以使用UriFactory.CreateDocumentCollectionUri(DatabaseName, CollectionName);

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);

不过,请注意您的练习. 对于返回大量文档的操作,建议您使用.AsDocumentQuery()方法,然后在query.HasMoreResults时执行ExecuteNextAsync.

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.

之所以应该这样做,是因为如果仅在IQueryable上使用.ToList()的话,CosmosDB SDK确实会通过电线同步跳闸,这将对应用程序的性能造成不利影响.

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.

这里是MSDN上的示例,介绍了如何做到这一点.

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

这篇关于获取集合CosmosDb中的所有文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆