c# - 限制 CosmosDB 查询的 1000 条记录

标签 c# azure azure-cosmosdb

我需要将对 CosmosDB 的查询结果限制为 1000 条记录,我试图将提要迭代器设置为在结果列表达到 1000 时退出,但现在它在 100 条记录后停止。这是我当前的代码:

        public async Task<IEnumerable<AS2InboundLogRecord>> RetrieveInboundLogs(string partitionKeyName, DateTime startDate, DateTime endDate)
        {
            var inboundLogs = new List<AS2InboundLogRecord>();
            string continuationToken = null;
            int itemLimit = 1000;

            QueryRequestOptions requestOptions = new QueryRequestOptions()
            {
                MaxItemCount = 100
            };


            using (FeedIterator<AS2InboundLogRecord> setIterator = dbContainer.GetItemLinqQueryable<AS2InboundLogRecord>(true, continuationToken, requestOptions)
                    .Where(x => (x.PartitionKey == partitionKeyName || partitionKeyName == null) &&
                      (x.MessageDate >= startDate) &&
                      (x.MessageDate <= endDate))
                    .ToFeedIterator())
            {
                while (setIterator.HasMoreResults)
                {
                    FeedResponse<AS2InboundLogRecord> response = await setIterator.ReadNextAsync();
                    inboundLogs.AddRange(response);

                    if (response.Count >= itemLimit) { break; }
                }

                Console.WriteLine(inboundLogs.Count());
                return inboundLogs.OrderByDescending(x => x.MessageDate);
            };
        }

任何意见都会被采纳,谢谢

最佳答案

我认为你必须纠正两件事:

首先:您将 limitCount 设置为 100 听起来好像只提取 100 条记录,如果此限制提取计数将其设置为 1000 否则转到第二个短语。

第二:您的 if 条件可能不起作用,因为您比较 response.CountinboundLogs.Count 应该进行比较。

更正:

public async Task<IEnumerable<AS2InboundLogRecord>> RetrieveInboundLogs(string partitionKeyName, DateTime startDate, DateTime endDate)
{
    var inboundLogs = new List<AS2InboundLogRecord>();
    string continuationToken = null;
    int itemLimit = 1000;

    QueryRequestOptions requestOptions = new QueryRequestOptions()
    {
        MaxItemCount = 10000
    };


    using (FeedIterator<AS2InboundLogRecord> setIterator = dbContainer.GetItemLinqQueryable<AS2InboundLogRecord>(true, continuationToken, requestOptions)
            .Where(x => (x.PartitionKey == partitionKeyName || partitionKeyName == null) &&
                (x.MessageDate >= startDate) &&
                (x.MessageDate <= endDate))
            .ToFeedIterator())
    {
        while (setIterator.HasMoreResults)
        {
            FeedResponse<AS2InboundLogRecord> response = await setIterator.ReadNextAsync();
            inboundLogs.AddRange(response);

            if (inboundLogs.Count >= itemLimit) { break; }
        }

        Console.WriteLine(inboundLogs.Count());
        return inboundLogs.OrderByDescending(x => x.MessageDate);
    };
}

由于 cosmosdb 不支持 SkipTake,您还可以使用 offsetlimit 描述here 或者您也可以使用 responseContinuationToken 来确保仍然有项目,例如:

do
{ 
    response = await setIterator.ReadNextAsync();
    //List<dynamic> documents = response.Resource.ToList();
    continuationToken = response.ContinuationToken;
    inboundLogs.AddRange(response);
} while (continuationToken != null && inboundLogs.Count <= itemLimit);

关于c# - 限制 CosmosDB 查询的 1000 条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71754265/

相关文章:

python - Flask Docker 镜像在 Azure 上出现超时错误

Azure Cosmos DB 数据迁移工具无法连接到 Cosmos DB

c# - 如何将 Action 提取到成员函数中?

c# - 内存映射文件 IList 实现,用于存储大型数据集 "in memory"?

c# - ASP.NET Membership.ValidateUser() 总是返回 "false"

azure - 在 azure adf 管道中选择查询

azure - 切换到 Azure SignalR 服务后 Context.UserIdentifier 为 null

c# - 在不使用指针的情况下编辑文本文件中的一行?

azure - 当 CosmosDB 集合文档具有不同属性时,如何使用 Azure 数据工厂将旧 CosmosDB 数据存档到 Azure 表?

azure - DocumentDB 查询 Array_Length > 1 的文档