mongo C# 根据id删除多条记录

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

它没有给出任何错误,但也没有删除任何记录。 有人帮忙吗?

您需要 In filter method to match the id values in a list, which is an implementation of the mongodb $in 查询

,而不是 Eq 过滤器
protected FilterDefinition<T> ItemWithListOfId(List<ObjectId> id)
{
    return Builders<T>.Filter.In("_id", id);            
}