Basically what I'm trying to do is a deleteMany based on a list of items that I have using C# and linq for a mongoDB.
This is my code:
// 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);
My code will not compile because on the lambda inside the IN filter shows this error:
var filter = Builders<Boletin>.Filter.In(item => item._id, boletinesToDelete);
CS1660: Cannot convert lambda expression to type X because it is not a delegate type
Did some research and the error is supposed to be "This error occurs if you try to assign or otherwise convert an anonymous method block to a type which is not a delegate type". But I'm not sure I get it since I'm not that familiar with delegate type
Please help
CodePudding user response:
From memory, I think an intersect may work better for this.
var filter = Builders<Boletin>.Intersect(boletinesToDelete);
CodePudding user response:
boletinesToDelete
should be a List<ObjectId>
or a List<string>
according to your item._id
not a List<Boletin>
.
db.collection.find({
_id: {
"$in": [
ObjectId("5a934e000102030405000000"),
ObjectId("5a934e000102030405000001")
]
}
})