基本上,我想要做的是基于我對 mongoDB 使用 C# 和 linq 的專案串列的 deleteMany。
這是我的代碼:
// List of elements to delete, read from the POST request
string jsonBoletines = await new StreamReader(req.Body).ReadToEndAsync();
// Convert the JSON into a list of "Boletin" objects, which is the type of objects I want //to delete from the collection
List <Boletin> boletinesToDelete = JsonConvert.DeserializeObject<List<Boletin>>(jsonBoletines);
// Create the filter using IN, I'm trying to delete using the _id
var filter = Builders<Boletin>.Filter.In(item => item._id, boletinesToDelete);
var results = collection.DeleteMany(filter);
我的代碼將無法編譯,因為在 IN 過濾器內的 lambda 上顯示此錯誤:
var filter = Builders<Boletin>.Filter.In(item => item._id, boletinesToDelete);
CS1660:無法將 lambda 運算式轉換為型別 X,因為它不是委托型別
做了一些研究,錯誤應該是“如果您嘗試將匿名方法塊分配或以其他方式轉換為不是委托型別的型別,則會發生此錯誤”。但我不確定我是否明白,因為我對委托型別不太熟悉
請幫忙
uj5u.com熱心網友回復:
根據記憶,我認為相交可能對此更有效。
var filter = Builders<Boletin>.Intersect(boletinesToDelete);
uj5u.com熱心網友回復:
boletinesToDelete應該是 aList<ObjectId>或 aList<string>根據您的item._idnot a List<Boletin>。
db.collection.find({
_id: {
"$in": [
ObjectId("5a934e000102030405000000"),
ObjectId("5a934e000102030405000001")
]
}
})
mongoplayground
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/340873.html
上一篇:【18】資料可視化+爬蟲:基于 Echarts + Python 實作的動態實時大屏范例 - 行業搜索指數排行榜
