[英]mongo C# remove multiple records by id
我正在嘗試在 mongo 中創建一個 deleteAll 方法,我可以在其中一次刪除多個記錄,但提供該方法要刪除的對象 ID 列表,如下所示
protected virtual DeleteResult DeleteAll(List<ObjectId> listId, WriteConcern concern = null)
{
return MongoCollection
.DeleteManyAsync(ItemWithListOfId(listId))
.Result;
}
protected FilterDefinition<T> ItemWithListOfId(List<ObjectId> id)
{
return Builders<T>.Filter.Eq("_id", id);
}
它沒有給出錯誤,但也沒有刪除任何記錄。 任何人有幫助嗎?
對於mongo版本1.10,我嘗試了以下代碼片段。
public void DeleteMany(List<string> entityIdList)
{
var bulkOp = this.Collection.InitializeUnorderedBulkOperation();
IMongoQuery query = null;
foreach (var entity in entityIdList)
{
ObjectId entityId;
if (ObjectId.TryParse(entity, out entityId))
{
query = query == null ? Query.EQ("_id", entityId) : Query.Or(query, Query.EQ("_id", entityId));
}
}
if (query != null)
{
bulkOp.Find(query).Remove();
bulkOp.Execute();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.