簡體   English   中英

Mongo C#驅動程序查找多個並刪除

[英]Mongo C# driver find multiple and delete

我正在使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM