繁体   English   中英

限制来自 CosmosDB 查询的 1000 条记录

[英]Limit 1000 Records from CosmosDB Query

我需要将对 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 ,否则转到Second短语。

第二:你的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 ,或者您也可以使用responseContinuationToken来确保仍有项目,例如:

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

暂无
暂无

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

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