【发布时间】:2017-11-30 22:31:01
【问题描述】:
我想在 Mongo 中进行批量更新插入。基本上我从供应商那里得到了一个对象列表,但我不知道我之前得到了哪些(并且需要更新)以及哪些是新的。我可以逐个进行 upsert,但 UpdateMany 不适用于 upsert 选项。
所以我选择了文档,在 C# 中更新,然后进行批量插入。
public async Task BulkUpsertData(List<MyObject> newUpsertDatas)
{
var usernames = newUpsertDatas.Select(p => p.Username);
var filter = Builders<MyObject>.Filter.In(p => p.Username, usernames);
//Find all records that are in the list of newUpsertDatas (these need to be updated)
var collection = Db.GetCollection<MyObject>("MyCollection");
var existingDatas = await collection.Find(filter).ToListAsync();
//loop through all of the new data,
foreach (var newUpsertData in newUpsertDatas)
{
//and find the matching existing data
var existingData = existingDatas.FirstOrDefault(p => p.Id == newUpsertData.Id);
//If there is existing data, preserve the date created (there are other fields I preserve)
if (existingData == null)
{
newUpsertData.DateCreated = DateTime.Now;
}
else
{
newUpsertData.Id = existingData.Id;
newUpsertData.DateCreated = existingData.DateCreated;
}
}
await collection.DeleteManyAsync(filter);
await collection.InsertManyAsync(newUpsertDatas);
}
有没有更有效的方法来做到这一点?
编辑:
我做了一些速度测试。
在准备过程中,我插入了一个非常简单的对象的 100,000 条记录。然后我将 200,000 条记录插入到集合中。
方法1如问题中所述。 SelectMany,代码更新,DeleteMany,InsertMany。这大约需要 5 秒。
方法 2 使用 Upsert = true 制作 UpdateOneModel 列表,然后执行 BulkWriteAsync。这超级慢。我可以看到 mongo 集合中的计数在增加,所以我知道它正在工作。但大约 5 分钟后,它只攀升到 107,000,所以我取消了它。
如果其他人有潜在的解决方案,我仍然感兴趣
【问题讨论】: