2012-02-24 88 views
3

我想知道是否有人有一個乾淨的方式來處理使用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(); 
} 

謝謝。

回答

0

最簡單的方法是將標準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; 
} 
+2

確認如何爲與LINQ表達式刪除?這是真正的問題;創建查詢。 – rboarman 2012-03-18 18:03:24