前言 :
在 MongoDB 的 API 裡, 你可以使用 collection.update() 來更新一個已經存在的 document. 底下的 Tutorial 就透過一些 user cases 來說明如何對 MongoDB 進行 update document.
Note :
在下面的範例, collection 中已經有以下 documents :
- {
- "hosting" : "hostA",
- "type" : "vps",
- "clients" : 1000
- },
- {
- "hosting" : "hostB",
- "type" : "dedicated server",
- "clients" : 100
- },
- {
- "hosting" : "hostC",
- "type" : "vps",
- "clients" : 900
- }
底下有 4 個 user case 對 collection 更新的範例. 範例中因為涉及對 collection 進行搜尋並更新, 固並不保證現有 collection 中的文件一定會被更新.
- Example 1
找出 hosting = hostB, 並用新的 document 來 update 它 :
- BasicDBObject newDocument = new BasicDBObject();
- newDocument.put("hosting", "hostB");
- newDocument.put("type", "shared host");
- newDocument.put("clients", 111);
- collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument);
- Example 2
這個範例使用了 "$inc" 的 modifier 來 increase 某個特定 field 的值 : Find hosting = hostB, update the "clients" value by increasing its value from 100 to 199, (100 + 99) = 199 :
- BasicDBObject newDocument = new BasicDBObject().append("$inc", new BasicDBObject().append("clients", 99));
- collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument);
- Example 3
這個範例使用 "$set" modifier 來更新特定 field 的值, 所以你就不用提供完整的 document : Find hosting = hostA, update the "type" from "vps" to "dedicated server" :
- BasicDBObject newDocument3 = new BasicDBObject().append("$set", new BasicDBObject().append("type", "dedicated server"));
- collection.update(new BasicDBObject().append("hosting", "hostA"), newDocument3);
Note : Why need to use $set?
- 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 :
- //find type = vps , update all matched documents , "clients" value to 888
- BasicDBObject updateQuery = new BasicDBObject().append("$set", new BasicDBObject().append("clients", "888"));
- //both methods are doing the same thing.
- //collection.updateMulti(new BasicDBObject().append("type", "vps"), updateQuery);
- collection.update(new BasicDBObject().append("type", "vps"), updateQuery, false, true);
Note : Why need to use “multi”?
完整代碼 :
底下是上述 4 個範例代碼的完整版 :
- package ir.stat.test.mongodb;
- import com.mongodb.BasicDBObject;
- import com.mongodb.DB;
- import com.mongodb.DBCollection;
- import com.mongodb.DBCursor;
- import com.mongodb.Mongo;
- public class UpdateEx {
- public static String MangoDB_IP = "192.168.80.168";
- public static String DB_NAME = "test";
- public static Mongo m = null;
- public static void dropCollection(String name) throws Exception
- {
- if(m==null) m = new Mongo(MangoDB_IP);
- DB db = m.getDB(DB_NAME);
- DBCollection coll = db.getCollection(name);
- coll.drop();
- }
- public static DBCollection retvCollection(String name) throws Exception {
- if(m==null) m = new Mongo(MangoDB_IP);
- DB db = m.getDB(DB_NAME);
- DBCollection coll = db.getCollection(name);
- return coll;
- }
- public static void insert(DBCollection coll, String hosting, String type, int clients)
- {
- BasicDBObject doc = new BasicDBObject();
- doc.put("hosting", hosting);
- doc.put("type", type);
- doc.put("clients", clients);
- coll.insert(doc);
- }
- public static void showCollection(DBCollection coll)
- {
- DBCursor cursorDocJSON = coll.find();
- while (cursorDocJSON.hasNext()) {
- System.out.println(cursorDocJSON.next());
- }
- }
- // A normal way to update an existing document. Find hosting = hostB, and update it with a new document.
- public static void exam01(DBCollection collection) throws Exception
- {
- BasicDBObject newDocument = new BasicDBObject();
- newDocument.put("hosting", "hostB");
- newDocument.put("type", "shared host");
- newDocument.put("clients", 111);
- collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument);
- }
- // Find hosting = hostB, update the "clients" value by increasing its value
- public static void exam02(DBCollection collection) throws Exception
- {
- BasicDBObject newDocument = new BasicDBObject().append("$inc", new BasicDBObject().append("clients", 99));
- collection.update(new BasicDBObject().append("hosting", "hostB"), newDocument);
- }
- // Find hosting = hostA, update the “type” from “vps” to “dedicated server”.
- public static void exam03(DBCollection collection) throws Exception
- {
- BasicDBObject newDocument3 = new BasicDBObject().append("$set", new BasicDBObject().append("type", "dedicated server"));
- collection.update(new BasicDBObject().append("hosting", "hostA"), newDocument3);
- }
- public static void exam04(DBCollection collection) throws Exception
- {
- //find type = vps , update all matched documents , "clients" value to 888
- BasicDBObject updateQuery = new BasicDBObject().append("$set", new BasicDBObject().append("clients", "888"));
- //both methods are doing the same thing.
- //collection.updateMulti(new BasicDBObject().append("type", "vps"), updateQuery);
- collection.update(new BasicDBObject().append("type", "vps"), updateQuery, false, true);
- }
- public static void main(String args[]) throws Exception
- {
- dropCollection("tutorial_update"); // Reset collection "tutorial_update"
- DBCollection coll = retvCollection("tutorial_update");
- insert(coll,"hostA" ,"vps" ,1000);
- insert(coll,"hostB" ,"dedicated server" ,100);
- insert(coll,"hostB" ,"vps" ,900);
- exam04(coll); showCollection(coll);
- }
- }
沒有留言:
張貼留言