Novice
Novice

Reputation: 478

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?

Upvotes: 7

Views: 7433

Answers (1)

chridam
chridam

Reputation: 103375

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

Upvotes: 10

Related Questions