【发布时间】:2020-11-02 19:29:51
【问题描述】:
我正在使用 MongoDB c# 驱动程序来实现以下目标:对于满足特定条件的所有记录,将记录日期字段之一设置为另一个日期字段的值。
我希望为此使用UpdateAllAsync,但似乎没有方便的方法来做到这一点。
所以现在我想知道使用ForEachAsync 与使用ToListAsync:
await this.repository.Find(filter).ForEachAsync(async record =>
{
await this.repository.UpdateOneAsync(
Builders<Records>.Filter.Eq(x => x.Id, record.Id),
Builders<Records>.Update.Set(x => x.Date1, record.Date2));
});
对
var records = await this.repository.Find(filter).ToListAsync();
foreach (var record in records)
{
await this.repository.UpdateOneAsync(
Builders<Records>.Filter.Eq(x => x.Id, record.Id),
Builders<Records>.Update.Set(x => x.Date1, record.Date2));
}
第一种方法安全吗?在这种情况下哪种方法更好?
【问题讨论】:
标签: c# mongodb mongodb-.net-driver