我正在使用 collection.FindOneAndDeleteAsync,但这在用于获取许多文档时会使用大量 CPU。使用 c# mongo 驱动程序查找多个文档(从 100 到 50k 的任何位置)并删除的最佳方法是什么?
谢谢
您需要Find删除要删除的文档,然后使用DeleteMany过滤器删除它们_id: {$in: ids},其中ids是_id这些文档值的可枚举项。
C# 示例:
public class Entity
{
public ObjectId id { get; set; }
public string name { get; set; }
}
// Find the documents to delete
var test = db.GetCollection<Entity>("test");
var filter = new BsonDocument();
var docs = test.Find(filter).ToList();
// Get the _id values of the found documents
var ids = docs.Select(d => d.id);
// Create an $in filter for those ids
var idsFilter = Builders<Entity>.Filter.In(d => d.id, ids);
// Delete the documents using the $in filter
var result = test.DeleteMany(idsFilter);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4602 次 |
| 最近记录: |