2012年6月1日 星期五

[ MongoDB 教學 ] Java MongoDB : Update document

來源自 這裡 
前言 : 
在 MongoDB 的 API 裡, 你可以使用 collection.update() 來更新一個已經存在的 document. 底下的 Tutorial 就透過一些 user cases 來說明如何對 MongoDB 進行 update document. 
Note : 
Before proceed on this tutorial, you should understand how-to-do, update in mongoDB, list of supported update modifiers, and the Java DBCollection update()method.

在下面的範例, collection 中已經有以下 documents : 
  1. {  
  2.     "hosting" : "hostA",  
  3.     "type" : "vps",  
  4.     "clients" : 1000  
  5. },  
  6. {  
  7.     "hosting" : "hostB",  
  8.     "type" : "dedicated server",  
  9.     "clients" : 100  
  10. },  
  11. {  
  12.     "hosting" : "hostC",  
  13.     "type" : "vps",  
  14.     "clients" : 900  
  15. }  
DBCollection.update() examples : 
底下有 4 個 user case 對 collection 更新的範例. 範例中因為涉及對 collection 進行搜尋並更新, 固並不保證現有 collection 中的文件一定會被更新. 

- Example 1 
找出 hosting = hostB, 並用新的 document 來 update 它 : 
  1. BasicDBObject newDocument = new BasicDBObject();  
  2. newDocument.put("hosting""hostB");  
  3. newDocument.put("type""shared host");  
  4. newDocument.put("clients"111);  
  5.   
  6. collection.update(new BasicDBObject().append("hosting""hostB"), newDocument);  
執行結果如下 : 
{ "_id" : { "$oid" : "4fc6d170c28ffdebd46236af"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
{ "_id" : { "$oid" : "4fc6d170c28ffdebd46236b0"} , "hosting" : "hostB" , "type" : "shared host" , "clients" : 111}
{ "_id" : { "$oid" : "4fc6d170c28ffdebd46236b1"} , "hosting" : "hostB" , "type" : "vps" , "clients" : 900}

- Example 2 
這個範例使用了 "$inc" 的 modifier 來 increase 某個特定 field 的值 : Find hosting = hostB, update the "clients" value by increasing its value from 100 to 199, (100 + 99) = 199 : 
  1. BasicDBObject newDocument = new BasicDBObject().append("$inc"new BasicDBObject().append("clients"99));  
  2.   
  3. collection.update(new BasicDBObject().append("hosting""hostB"), newDocument);  
執行結果如下 : 
{ "_id" : { "$oid" : "4fc6d6a4c28f951ffeba5e1d"} , "hosting" : "hostA" , "type" : "vps" , "clients" : 1000}
{ "_id" : { "$oid" : "4fc6d6a4c28f951ffeba5e1e"} , "hosting" : "hostB" , "type" : "dedicated server" ,
 "clients" : 199}
{ "_id" : { "$oid" : "4fc6d6a4c28f951ffeba5e1f"} , "hosting" : "hostB" , "type" : "vps" , "clients" : 900}

- Example 3 
這個範例使用 "$set" modifier 來更新特定 field 的值, 所以你就不用提供完整的 document : Find hosting = hostA, update the "type" from "vps" to "dedicated server" : 
  1. BasicDBObject newDocument3 = new BasicDBObject().append("$set"new BasicDBObject().append("type""dedicated server"));  
  2.   
  3. collection.update(new BasicDBObject().append("hosting""hostA"), newDocument3);  
執行結果如下 : 
{ "_id" : { "$oid" : "4fc6d82bc28fe581466794f9"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100}
{ "_id" : { "$oid" : "4fc6d82bc28fe581466794fa"} , "hosting" : "hostB" , "type" : "vps" , "clients" : 900}
{ "_id" : { "$oid" : "4fc6d82bc28fe581466794f8"} , "clients" : 1000 , "hosting" : "hostA" , "type" : "dedicated server"}

Note : Why need to use $set? 
By default, if you update a document like this :
  1. BasicDBObject newDocument3 = new BasicDBObject().append("type""dedicated server");  
  2.   
  3. collection.update(new BasicDBObject().append("hosting""hostA"), newDocument3);  
It will replaced the entire old document, see following result :
{"_id":{ "$oid" : "x"} , "type" : "dedicated server"}
{"_id":{ "$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100}
{"_id":{ "$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}

With “$set” modifier, only particular value is updated.

- Example 4 
這個範例設定 "multi" 參數讓你可以同時 update 多個 matched query 的 documents. 使用方法為 update(DBObject q, DBObject o, boolean upsert, boolean multi) : Find type = "vps", update all the matched documents, "clients" value to 888 : 
* upsert - if the database should create the element if it does not exist
* multi - if the update should be applied to all objects matching

  1. //find type = vps , update all matched documents , "clients" value to 888  
  2. BasicDBObject updateQuery = new BasicDBObject().append("$set",  new BasicDBObject().append("clients""888"));  
  3.   
  4. //both methods are doing the same thing.  
  5. //collection.updateMulti(new BasicDBObject().append("type", "vps"), updateQuery);  
  6. collection.update(new BasicDBObject().append("type""vps"), updateQuery, falsetrue);  
執行結果如下 : 
{ "_id" : { "$oid" : "4fc6dafbc28f926c06f04c6f"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100}
{ "_id" : { "$oid" : "4fc6dafbc28f926c06f04c6e"} ,
 "clients" : "888" , "hosting" : "hostA" , "type" : "vps"}
{ "_id" : { "$oid" : "4fc6dafbc28f926c06f04c70"} ,
 "clients" : "888" , "hosting" : "hostB" , "type" : "vps"}

Note : Why need to use “multi”? 
If you update a document without the “multi” parameter like this :
  1. BasicDBObject updateQuery = new BasicDBObject().append("$set"new BasicDBObject().append("total clients""888"));  
  2.   
  3.        //multi default to false  
  4. collection.update(new BasicDBObject().append("type""vps"), updateQuery);  
You will noticed that only the first matched document is updated.
{"_id":{ "$oid" : "x"} , "hosting" : "hostA" , "clients" : "888" , "type" : "vps"}
{"_id":{ "$oid" : "x"} , "hosting" : "hostB" , "type" : "dedicated server" , "clients" : 100}
{"_id":{ "$oid" : "x"} , "hosting" : "hostC" , "type" : "vps" , "clients" : 900}

To update a set of matched documents, you need to set “multi” to true.

完整代碼 : 
底下是上述 4 個範例代碼的完整版 : 
  1. package ir.stat.test.mongodb;  
  2.   
  3. import com.mongodb.BasicDBObject;  
  4. import com.mongodb.DB;  
  5. import com.mongodb.DBCollection;  
  6. import com.mongodb.DBCursor;  
  7. import com.mongodb.Mongo;  
  8.   
  9. public class UpdateEx {  
  10.     public static String MangoDB_IP = "192.168.80.168";  
  11.     public static String DB_NAME = "test";  
  12.     public static Mongo m = null;  
  13.   
  14.     public static void dropCollection(String name) throws Exception  
  15.     {  
  16.         if(m==null) m = new Mongo(MangoDB_IP);  
  17.         DB db = m.getDB(DB_NAME);  
  18.         DBCollection coll = db.getCollection(name);  
  19.         coll.drop();  
  20.     }  
  21.       
  22.     public static DBCollection retvCollection(String name) throws Exception {  
  23.         if(m==null) m = new Mongo(MangoDB_IP);  
  24.         DB db = m.getDB(DB_NAME);  
  25.   
  26.         DBCollection coll = db.getCollection(name);  
  27.         return coll;  
  28.     }  
  29.       
  30.     public static void insert(DBCollection coll, String hosting, String type, int clients)  
  31.     {  
  32.         BasicDBObject doc = new BasicDBObject();  
  33.         doc.put("hosting", hosting);  
  34.         doc.put("type", type);  
  35.         doc.put("clients", clients);  
  36.         coll.insert(doc);  
  37.     }  
  38.       
  39.     public static void showCollection(DBCollection coll)  
  40.     {  
  41.         DBCursor cursorDocJSON = coll.find();  
  42.         while (cursorDocJSON.hasNext()) {  
  43.             System.out.println(cursorDocJSON.next());  
  44.         }  
  45.     }  
  46.       
  47.     // A normal way to update an existing document. Find hosting = hostB, and update it with a new document.  
  48.     public static void exam01(DBCollection collection) throws Exception  
  49.     {  
  50.         BasicDBObject newDocument = new BasicDBObject();  
  51.         newDocument.put("hosting""hostB");  
  52.         newDocument.put("type""shared host");  
  53.         newDocument.put("clients"111);  
  54.        
  55.         collection.update(new BasicDBObject().append("hosting""hostB"), newDocument);  
  56.     }     
  57.       
  58.     // Find hosting = hostB, update the "clients" value by increasing its value  
  59.     public static void exam02(DBCollection collection) throws Exception  
  60.     {  
  61.         BasicDBObject newDocument = new BasicDBObject().append("$inc"new BasicDBObject().append("clients"99));  
  62.   
  63.         collection.update(new BasicDBObject().append("hosting""hostB"), newDocument);  
  64.     }  
  65.       
  66.     // Find hosting = hostA, update the “type” from “vps” to “dedicated server”.  
  67.     public static void exam03(DBCollection collection) throws Exception  
  68.     {  
  69.         BasicDBObject newDocument3 = new BasicDBObject().append("$set"new BasicDBObject().append("type""dedicated server"));  
  70.            
  71.         collection.update(new BasicDBObject().append("hosting""hostA"), newDocument3);  
  72.     }  
  73.       
  74.     public static void exam04(DBCollection collection) throws Exception  
  75.     {  
  76.         //find type = vps , update all matched documents , "clients" value to 888  
  77.         BasicDBObject updateQuery = new BasicDBObject().append("$set",  new BasicDBObject().append("clients""888"));  
  78.        
  79.         //both methods are doing the same thing.  
  80.         //collection.updateMulti(new BasicDBObject().append("type", "vps"), updateQuery);  
  81.         collection.update(new BasicDBObject().append("type""vps"), updateQuery, falsetrue);  
  82.     }  
  83.       
  84.     public static void main(String args[]) throws Exception  
  85.     {  
  86.         dropCollection("tutorial_update");  // Reset collection "tutorial_update"  
  87.         DBCollection coll = retvCollection("tutorial_update");        
  88.         insert(coll,"hostA" ,"vps" ,1000);  
  89.         insert(coll,"hostB" ,"dedicated server" ,100);  
  90.         insert(coll,"hostB" ,"vps" ,900);  
  91.           
  92.         exam04(coll); showCollection(coll);  
  93.     }  
  94. }  

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...