前言 :
底下將介紹 4 種方法透過 MongoDB API 將下面 JSON 數據插入到數據庫中 :
- {
- "database" : "mkyongDB",
- "table" : "hosting",
- "detail" :
- {
- records : 99,
- index : "vps_index1",
- active : "true"
- }
- }
- }
底下會有四種方法, 讓你可以將 JSON 結構的數據插入到 MongoDB 中, 而在所有範例中, 都是透過下面方法取出 MangoDB 中的 collection. 代碼如下 :
- public static DBCollection retvCollection(String name) throws Exception
- {
- Mongo m = new Mongo( "192.168.80.166" ); # 假設安裝 MongoDB machine 的 IP 為 "192.168.80.166"
- DB db = m.getDB("test"); # 連接 database="test"
- DBCollection coll = db.getCollection(name);
- return coll;
- }
- DBCollection collection = retvCollection("tutorial"); # 取出名為 "tutorial" 的 collection
透過物件 BasicDBObject 的父類別 BasicBSONObject 上面的方法 put() 封裝 JSON 數據 :
- BasicDBObject document = new BasicDBObject();
- document.put("database", "mkyongDB");
- document.put("table", "hosting");
- BasicDBObject documentDetail = new BasicDBObject();
- documentDetail.put("records", "99");
- documentDetail.put("index", "vps_index1");
- documentDetail.put("active", "true");
- document.put("detail", documentDetail);
- collection.insert(document);
- BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()
- .add("database", "mkyongDB")
- .add("table", "hosting");
- BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start()
- .add("records", "99")
- .add("index", "vps_index1")
- .add("active", "true");
- documentBuilder.add("detail", documentBuilderDetail.get());
- collection.insert(documentBuilder.get());
- Map
documentMap = new HashMap(); - documentMap.put("database", "mkyongDB");
- documentMap.put("table", "hosting");
- Map
documentMapDetail = new HashMap (); - documentMapDetail.put("records", "99");
- documentMapDetail.put("index", "vps_index1");
- documentMapDetail.put("active", "true");
- documentMap.put("detail", documentMapDetail);
- collection.insert(new BasicDBObject(documentMap));
- String json = "{'database' : 'mkyongDB','table' : 'hosting'," +
- "'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}";
- DBObject dbObject = (DBObject)JSON.parse(json);
- collection.insert(dbObject);
底下是上述 4 個範例代碼的完整版 :
- package ir.stat.test.mongodb;
- import java.util.HashMap;
- import java.util.Map;
- import com.mongodb.BasicDBObject;
- import com.mongodb.BasicDBObjectBuilder;
- import com.mongodb.DB;
- import com.mongodb.DBCollection;
- import com.mongodb.DBCursor;
- import com.mongodb.DBObject;
- import com.mongodb.Mongo;
- import com.mongodb.util.JSON;
- public class InsertEx {
- public static String MangoDB_IP = "192.168.80.167";
- public static String DB_NAME = "test";
- public static Mongo m = null;
- public static DBCollection retvCollection(String name) throws Exception {
- Mongo m = new Mongo(MangoDB_IP);
- DB db = m.getDB(DB_NAME);
- DBCollection coll = db.getCollection(name);
- return coll;
- }
- // BasicDBObject example
- public static void exam01(DBCollection collection) throws Exception {
- BasicDBObject document = new BasicDBObject();
- document.put("database", "mkyongDB");
- document.put("table", "hosting");
- BasicDBObject documentDetail = new BasicDBObject();
- documentDetail.put("records", "99");
- documentDetail.put("index", "vps_index1");
- documentDetail.put("active", "true");
- document.put("detail", documentDetail);
- collection.insert(document);
- }
- // BasicDBObjectBuilder example
- public static void exam02(DBCollection collection) throws Exception {
- BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()
- .add("database", "mkyongDB").add("table", "hosting");
- BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder
- .start().add("records", "99").add("index", "vps_index1")
- .add("active", "true");
- documentBuilder.add("detail", documentBuilderDetail.get());
- collection.insert(documentBuilder.get());
- }
- // Map example
- public static void exam03(DBCollection collection) throws Exception {
- Map
documentMap = new HashMap (); - documentMap.put("database", "mkyongDB");
- documentMap.put("table", "hosting");
- Map
documentMapDetail = new HashMap (); - documentMapDetail.put("records", "99");
- documentMapDetail.put("index", "vps_index1");
- documentMapDetail.put("active", "true");
- documentMap.put("detail", documentMapDetail);
- collection.insert(new BasicDBObject(documentMap));
- }
- // JSON parse example
- public static void exam04(DBCollection collection) throws Exception {
- String json = "{'database' : 'mkyongDB','table' : 'hosting',"
- + "'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}";
- DBObject dbObject = (DBObject) JSON.parse(json);
- collection.insert(dbObject);
- }
- public static void main(String args[]) throws Exception {
- DBCollection collection = retvCollection("tutorial");
- exam01(collection);
- exam02(collection);
- exam03(collection);
- exam04(collection);
- DBCursor cursorDocJSON = collection.find();
- while (cursorDocJSON.hasNext()) {
- System.out.println(cursorDocJSON.next());
- }
- collection.remove(new BasicDBObject());
- }
- }
沒有留言:
張貼留言