而不是为从查询中检索的每个文档向数据库发出请求。我们可以创建一个WriteModel操作列表。
var operationList = new List<WriteModel<JobInfoRecord>>();
using (var cursor = await jobInfoDocuments.Find(filter).Project(projectionDefinition).ToCursorAsync())
{
while (await cursor.MoveNextAsync())
{
var batch = cursor.Current;
foreach (var document in batch)
{
var newInfo = Regex.Replace(document.SerializedInfo, regex, EmptyTag);
// Applying several operations within the one request.
operationList.Add(new UpdateOneModel<JobInfoRecord>(Builders<JobInfoRecord>.Filter.Eq("_id", document.Id),
Builders<JobInfoRecord>.Update.Set("SerializedBackgroundJobInfo", newInfo)));
}
在每1000个清除列表中执行一次是不错的算法?
if (operationList.Count <= 1000)
{
await jobInfoDocuments.BulkWriteAsync(operationList, writeOptions);
operationList = new List<WriteModel<JobInfoRecord>>();
}
然后在while (await cursor.MoveNextAsync())
之后检查我们是否没有忘记任何内容
if (operationList.Count > 0)
{
await jobInfoDocuments.BulkWriteAsync(operationList, writeOptions);
}
或者我可以在没有任何foreach
语句的if
循环之后执行以下操作:
await jobInfoDocuments.BulkWriteAsync(operationList, writeOptions);
operationList = new List<WriteModel<JobInfoRecord>>();
foreach
循环后我该怎么办?
答案 0 :(得分:2)
您需要在foreach
// Execute once in every 1000 and clear list
if (operationList.Count == 1000)
{
await jobInfoDocuments.BulkWriteAsync(operationList, writeOptions);
operationList = new List<WriteModel<JobInfoRecord>>();
}
这是提高效果的最佳方式。
每组操作最多可以有1000次操作。如果一个组超过此限制,MongoDB会将该组划分为1000或更少的较小组。例如,如果批量操作列表包含2000个插入操作,MongoDB将创建2个组,每个组有1000个操作。