限制来自 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.Count
而 inboundLogs.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
不支持 Skip
和 Take
您也可以使用 offset
和 limit
描述的 here
或者您也可以使用 response
的 ContinuationToken
来确保仍然有项目,例如:
do
{
response = await setIterator.ReadNextAsync();
//List<dynamic> documents = response.Resource.ToList();
continuationToken = response.ContinuationToken;
inboundLogs.AddRange(response);
} while (continuationToken != null && inboundLogs.Count <= itemLimit);
我需要将对 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.Count
而 inboundLogs.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
不支持 Skip
和 Take
您也可以使用 offset
和 limit
描述的 here
或者您也可以使用 response
的 ContinuationToken
来确保仍然有项目,例如:
do
{
response = await setIterator.ReadNextAsync();
//List<dynamic> documents = response.Resource.ToList();
continuationToken = response.ContinuationToken;
inboundLogs.AddRange(response);
} while (continuationToken != null && inboundLogs.Count <= itemLimit);