Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo C# remove multiple records by id

I am trying to create a deleteAll method in mongo where I could delete multiple records in one go bu supplying the method a list of object ids to be deleted something like this

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);            
    }

it is giving no error but is not deleting any record as well. Anyone any help?

like image 542
Novice Avatar asked Sep 16 '15 07:09

Novice


1 Answers

Instead of the Eq filter, you need an In filter method to match the id values in a list, which is an implementation of the mongodb $in query

protected FilterDefinition<T> ItemWithListOfId(List<ObjectId> id)
{
    return Builders<T>.Filter.In("_id", id);            
}
like image 98
chridam Avatar answered Oct 19 '22 19:10

chridam