![](/img/trans.png)
[英]GeoSpatial queries working find in mongo shell but not filtering in C# driver
[英]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.