2012-12-17 55 views
1

我讀了MongoDB文檔,但我不太明白!我需要知道下面的代碼是否正確!如果手術成功進行,我需要確認。需要調用getLastError還是try-catch就足夠了?我正在使用正確的方式使用mongodb驅動程序執行此刪除代碼?

 public override bool DeleteUser(string username, bool deleteAllRelatedData) 
     { 
      WriteConcernResult result = null; 
      try 
      { 
       result = this.users.Remove(Query.And(Query.EQ("ApplicationName", 
        this.ApplicationName), Query.EQ("Username", username)), RemoveFlags.Single, 
        WriteConcern.Acknowledged); 

       if (result.HasLastErrorMessage) 
       { 
        return false; 
       } 

       return (result.DocumentsAffected == 1); 
      } 
      catch (Exception ex) 
      { 
       return false; 
      } 
     } 

回答

3

因爲您使用的是WriteConcern.Acknowledged,所以try/catch足夠好。 WriteConcern.Acknowledged會爲你做getLastError。

相關問題