2012年5月30日 星期三

[ MongoDB 教學 ] Java MongoDB : Insert a document

來源自 這裡 
前言 : 
底下將介紹 4 種方法透過 MongoDB API 將下面 JSON 數據插入到數據庫中 : 
  1. {  
  2.     "database" : "mkyongDB",  
  3.     "table" : "hosting",  
  4.     "detail" :   
  5.         {  
  6.             records : 99,  
  7.             index : "vps_index1",  
  8.             active : "true"  
  9.         }  
  10.     }  
  11. }  
數據 Insert 範例 : 
底下會有四種方法, 讓你可以將 JSON 結構的數據插入到 MongoDB 中, 而在所有範例中, 都是透過下面方法取出 MangoDB 中的 collection. 代碼如下 : 
  1. public static DBCollection retvCollection(String name) throws Exception  
  2. {  
  3.     Mongo m = new Mongo( "192.168.80.166" );  # 假設安裝 MongoDB machine 的 IP 為 "192.168.80.166"  
  4.     DB db = m.getDB("test"); # 連接 database="test"  
  5.       
  6.     DBCollection coll = db.getCollection(name);  
  7.     return coll;  
  8. }  
所以你就可以用下面代碼取出 collection : 
  1. DBCollection collection = retvCollection("tutorial"); # 取出名為 "tutorial" 的 collection  
- BasicDBObject example 
透過物件 BasicDBObject 的父類別 BasicBSONObject 上面的方法 put() 封裝 JSON 數據 : 
  1. BasicDBObject document = new BasicDBObject();  
  2. document.put("database""mkyongDB");  
  3. document.put("table""hosting");  
  4.   
  5. BasicDBObject documentDetail = new BasicDBObject();  
  6. documentDetail.put("records""99");  
  7. documentDetail.put("index""vps_index1");  
  8. documentDetail.put("active""true");  
  9.   
  10. document.put("detail", documentDetail);  
  11.   
  12. collection.insert(document);  
- BasicDBObjectBuilder example 
  1. BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()  
  2.     .add("database""mkyongDB")  
  3.     .add("table""hosting");  
  4.   
  5. BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start()  
  6. .add("records""99")  
  7. .add("index""vps_index1")  
  8. .add("active""true");  
  9.   
  10. documentBuilder.add("detail", documentBuilderDetail.get());  
  11.   
  12. collection.insert(documentBuilder.get());  
- Map example 
  1. Map documentMap = new HashMap();  
  2. documentMap.put("database""mkyongDB");  
  3. documentMap.put("table""hosting");  
  4.   
  5. Map documentMapDetail = new HashMap();  
  6. documentMapDetail.put("records""99");  
  7. documentMapDetail.put("index""vps_index1");  
  8. documentMapDetail.put("active""true");  
  9.   
  10. documentMap.put("detail", documentMapDetail);  
  11.   
  12. collection.insert(new BasicDBObject(documentMap));  
- JSON parse example 
  1. String json = "{'database' : 'mkyongDB','table' : 'hosting'," +  
  2.     "'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}";  
  3.   
  4. DBObject dbObject = (DBObject)JSON.parse(json);  
  5.   
  6. collection.insert(dbObject);  
完整代碼 : 
底下是上述 4 個範例代碼的完整版 : 
  1. package ir.stat.test.mongodb;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import com.mongodb.BasicDBObject;  
  7. import com.mongodb.BasicDBObjectBuilder;  
  8. import com.mongodb.DB;  
  9. import com.mongodb.DBCollection;  
  10. import com.mongodb.DBCursor;  
  11. import com.mongodb.DBObject;  
  12. import com.mongodb.Mongo;  
  13. import com.mongodb.util.JSON;  
  14.   
  15. public class InsertEx {  
  16.     public static String MangoDB_IP = "192.168.80.167";  
  17.     public static String DB_NAME = "test";  
  18.     public static Mongo m = null;  
  19.   
  20.     public static DBCollection retvCollection(String name) throws Exception {  
  21.         Mongo m = new Mongo(MangoDB_IP);  
  22.         DB db = m.getDB(DB_NAME);  
  23.   
  24.         DBCollection coll = db.getCollection(name);  
  25.         return coll;  
  26.     }  
  27.   
  28.     // BasicDBObject example  
  29.     public static void exam01(DBCollection collection) throws Exception {  
  30.         BasicDBObject document = new BasicDBObject();  
  31.         document.put("database""mkyongDB");  
  32.         document.put("table""hosting");  
  33.   
  34.         BasicDBObject documentDetail = new BasicDBObject();  
  35.         documentDetail.put("records""99");  
  36.         documentDetail.put("index""vps_index1");  
  37.         documentDetail.put("active""true");  
  38.   
  39.         document.put("detail", documentDetail);  
  40.   
  41.         collection.insert(document);  
  42.     }  
  43.   
  44.     // BasicDBObjectBuilder example  
  45.     public static void exam02(DBCollection collection) throws Exception {  
  46.         BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()  
  47.                 .add("database""mkyongDB").add("table""hosting");  
  48.   
  49.         BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder  
  50.                 .start().add("records""99").add("index""vps_index1")  
  51.                 .add("active""true");  
  52.   
  53.         documentBuilder.add("detail", documentBuilderDetail.get());  
  54.   
  55.         collection.insert(documentBuilder.get());  
  56.     }  
  57.   
  58.     // Map example  
  59.     public static void exam03(DBCollection collection) throws Exception {  
  60.         Map documentMap = new HashMap();  
  61.         documentMap.put("database""mkyongDB");  
  62.         documentMap.put("table""hosting");  
  63.   
  64.         Map documentMapDetail = new HashMap();  
  65.         documentMapDetail.put("records""99");  
  66.         documentMapDetail.put("index""vps_index1");  
  67.         documentMapDetail.put("active""true");  
  68.   
  69.         documentMap.put("detail", documentMapDetail);  
  70.   
  71.         collection.insert(new BasicDBObject(documentMap));  
  72.     }  
  73.   
  74.     // JSON parse example  
  75.     public static void exam04(DBCollection collection) throws Exception {  
  76.         String json = "{'database' : 'mkyongDB','table' : 'hosting',"  
  77.                 + "'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}";  
  78.   
  79.         DBObject dbObject = (DBObject) JSON.parse(json);  
  80.   
  81.         collection.insert(dbObject);  
  82.     }  
  83.   
  84.     public static void main(String args[]) throws Exception {  
  85.         DBCollection collection = retvCollection("tutorial");  
  86.         exam01(collection);  
  87.         exam02(collection);  
  88.         exam03(collection);  
  89.         exam04(collection);  
  90.   
  91.         DBCursor cursorDocJSON = collection.find();  
  92.         while (cursorDocJSON.hasNext()) {  
  93.             System.out.println(cursorDocJSON.next());  
  94.         }  
  95.   
  96.         collection.remove(new BasicDBObject());  
  97.     }  
  98. }  
執行結果如下 : 
{ "_id" : { "$oid" : "4fc5fb1f3df81ed5ee57331b"} , "database" : "mkyongDB" , "table" : "hosting" , "detail" : { "records" : "99" , "index" : "vps_index1" , "active" : "true"}}
{ "_id" : { "$oid" : "4fc5fb1f3df81ed5ee57331c"} , "database" : "mkyongDB" , "table" : "hosting" , "detail" : { "records" : "99" , "index" : "vps_index1" , "active" : "true"}}
{ "_id" : { "$oid" : "4fc5fb1f3df81ed5ee57331d"} , "detail" : { "index" : "vps_index1" , "active" : "true" , "records" : "99"} , "table" : "hosting" , "database" : "mkyongDB"}
{ "_id" : { "$oid" : "4fc5fb1f3df81ed5ee57331e"} , "database" : "mkyongDB" , "table" : "hosting" , "detail" : { "records" : 99 , "index" : "vps_index1" , "active" : "true"}}

[Git 文章收集] Differences between git merge and git rebase

Source From  Here Preface Merging and rebasing are the two most popular way to applying changes from one branch into another one. They bot...