使用FluentMongo删除和更新文档
问题描述:
我想知道是否有人有一个干净的方式来处理使用FluentMongo删除和更新文档?使用FluentMongo删除和更新文档
我正在使用FluentMongo创建存储库层;然而,无法删除或更新文件证明是麻烦的。也许我错过了一个方法来处理这个问题,同时保持适当的存储库模式?
public interface IRepository : IDisposable
{
IQueryable<T> All<T>() where T : class, new();
void Delete<T>(Expression<Func<T, bool>> expression)
where T : class, new();
void Update<TEntity>(TEntity entity) where TEntity : class, new();
}
谢谢。
答
最简单的方法是将标准MongoCollection包装到存储库方法后面。由于您的存储库是键入的,您可以创建一个类型化的集合并从该集合中删除文档。这是一个示例实现。
MongoCollection<T> collection = mongoserver.GetCollection<T>();
public void Delete(string id)
{
this.collection.Remove(Query.EQ("_id", id));
}
public void Delete(T entity)
{
this.Delete(entity.Id);
}
使用FluentMongo于2013年7月27日
加入由balexandre,那里检索MongoCollection<T>
是对高级查询有用,例如,如果我们想删除属性我们所有的我们收集的文件,我们会写这样的:
public void DeleteAll() {
var collection = myRepository.Collection;
collection.RemoveAll();
}
,如果你想返回所有的文件都删除确实使用Ok
财产
public bool DeleteAll() {
var collection = myRepository.Collection;
return collection.RemoveAll().Ok;
}
确认如何为与LINQ表达式删除?这是真正的问题;创建查询。 – rboarman 2012-03-18 18:03:24