MongoDB操做:update()

  1. @Override  數據庫

  2.     public boolean update(String dbName, String collectionName,  ide

  3.             DBObject oldValue, DBObject newValue) {  spa

  4.         DB db = null;  get

  5.         DBCollection dbCollection = null;  it

  6.         WriteResult result = null;  io

  7.         String resultString = null;  date

  8.           

  9.         if(oldValue.equals(newValue)){  exception

  10.             return true;  request

  11.         }else{  數據

  12.             try {  

  13.                 db = mongoClient.getDB(dbName); //獲取數據庫實例  

  14.                 dbCollection = db.getCollection(collectionName);    //獲取數據庫中指定的collection集合  

  15.                   

  16.                 result = dbCollection.update(oldValue, newValue);  

  17.                 resultString = result.getError();  

  18.                   

  19.                 return (resultString!=null) ? false : true;  

  20.             } catch (Exception e) {  

  21.                 // TODO: handle exception  

  22.                 e.printStackTrace();  

  23.             } finally{  

  24.                 if(null != db){  

  25.                     db.requestDone();   //關閉db  

  26.                     db = null;  

  27.                 }  

  28.             }  

  29.               

  30.         }  

  31.           

  32.         return false;  

  33.     }  

相關文章
相關標籤/搜索