1. [代码]方法1
01 | BasicDBObject document = new BasicDBObject(); |
02 | document.put( "database" , "mkyongDB" ); |
03 | document.put( "table" , "hosting" ); |
04 |
05 | BasicDBObject documentDetail = new BasicDBObject(); |
06 | documentDetail.put( "records" , "99" ); |
07 | documentDetail.put( "index" , "vps_index1" ); |
08 | documentDetail.put( "active" , "true" ); |
09 |
10 | document.put( "detail" , documentDetail); |
11 |
12 | collection.insert(document); |
2. [代码]方法2
01 | BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start() |
02 | .add( "database" , "mkyongDB" ) |
03 | .add( "table" , "hosting" ); |
04 |
05 | BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start() |
06 | .add( "records" , "99" ) |
07 | .add( "index" , "vps_index1" ) |
08 | .add( "active" , "true" ); |
09 |
10 | documentBuilder.add( "detail" , documentBuilderDetail.get()); |
11 |
12 | collection.insert(documentBuilder.get()); |
3. [代码]方法3(使用Map)
01 | Map<String, Object> documentMap = new HashMap<String, Object>(); |
02 | documentMap.put( "database" , "mkyongDB" ); |
03 | documentMap.put( "table" , "hosting" ); |
04 |
05 | Map<String, Object> documentMapDetail = new HashMap<String, Object>(); |
06 | documentMapDetail.put( "records" , "99" ); |
07 | documentMapDetail.put( "index" , "vps_index1" ); |
08 | documentMapDetail.put( "active" , "true" ); |
09 |
10 | documentMap.put( "detail" , documentMapDetail); |
11 |
12 | collection.insert( new BasicDBObject(documentMap)); |
4. [代码]方法4(使用JSON)
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); |
5. [代码]完整示例 InsertDocumentApp.java 跳至 [1] [2] [3] [4] [5] [全屏预览]
001 | package com.mkyong.core; |
002 | |
003 | import java.net.UnknownHostException; |
004 | import java.util.HashMap; |
005 | import java.util.Map; |
006 | import com.mongodb.BasicDBObject; |
007 | import com.mongodb.BasicDBObjectBuilder; |
008 | import com.mongodb.DB; |
009 | import com.mongodb.DBCollection; |
010 | import com.mongodb.DBCursor; |
011 | import com.mongodb.DBObject; |
012 | import com.mongodb.Mongo; |
013 | import com.mongodb.MongoException; |
014 | import com.mongodb.util.JSON; |
015 | |
016 | /** |
017 | * Java MongoDB : Insert a Document |
018 | * |
019 | */ |
020 | public class InsertDocumentApp { |
021 | public static void main(String[] args) { |
022 | |
023 | try { |
024 | |
025 | Mongo mongo = new Mongo( "localhost" , 27017 ); |
026 | DB db = mongo.getDB( "yourdb" ); |
027 | |
028 | // get a single collection |
029 | DBCollection collection = db.getCollection( "dummyColl" ); |
030 | |
031 | // BasicDBObject example |
032 | System.out.println( "BasicDBObject example..." ); |
033 | BasicDBObject document = new BasicDBObject(); |
034 | document.put( "database" , "mkyongDB" ); |
035 | document.put( "table" , "hosting" ); |
036 | |
037 | BasicDBObject documentDetail = new BasicDBObject(); |
038 | documentDetail.put( "records" , "99" ); |
039 | documentDetail.put( "index" , "vps_index1" ); |
040 | documentDetail.put( "active" , "true" ); |
041 | document.put( "detail" , documentDetail); |
042 | |
043 | collection.insert(document); |
044 | |
045 | DBCursor cursorDoc = collection.find(); |
046 | while (cursorDoc.hasNext()) { |
047 | System.out.println(cursorDoc.next()); |
048 | } |
049 | |
050 | collection.remove( new BasicDBObject()); |
051 | |
052 | // BasicDBObjectBuilder example |
053 | System.out.println( "BasicDBObjectBuilder example..." ); |
054 | BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start() |
055 | .add( "database" , "mkyongDB" ) |
056 | .add( "table" , "hosting" ); |
057 | |
058 | BasicDBObjectBuilder documentBuilderDetail = BasicDBObjectBuilder.start() |
059 | .add( "records" , "99" ) |
060 | .add( "index" , "vps_index1" ) |
061 | .add( "active" , "true" ); |
062 | |
063 | documentBuilder.add( "detail" , documentBuilderDetail.get()); |
064 | |
065 | collection.insert(documentBuilder.get()); |
066 | |
067 | DBCursor cursorDocBuilder = collection.find(); |
068 | while (cursorDocBuilder.hasNext()) { |
069 | System.out.println(cursorDocBuilder.next()); |
070 | } |
071 | |
072 | collection.remove( new BasicDBObject()); |
073 | |
074 | // Map example |
075 | System.out.println( "Map example..." ); |
076 | Map<String, Object> documentMap = new HashMap<String, Object>(); |
077 | documentMap.put( "database" , "mkyongDB" ); |
078 | documentMap.put( "table" , "hosting" ); |
079 | |
080 | Map<String, Object> documentMapDetail = new HashMap<String, Object>(); |
081 | documentMapDetail.put( "records" , "99" ); |
082 | documentMapDetail.put( "index" , "vps_index1" ); |
083 | documentMapDetail.put( "active" , "true" ); |
084 | |
085 | documentMap.put( "detail" , documentMapDetail); |
086 | |
087 | collection.insert( new BasicDBObject(documentMap)); |
088 | |
089 | DBCursor cursorDocMap = collection.find(); |
090 | while (cursorDocMap.hasNext()) { |
091 | System.out.println(cursorDocMap.next()); |
092 | } |
093 | |
094 | collection.remove( new BasicDBObject()); |
095 | |
096 | // JSON parse example |
097 | System.out.println( "JSON parse example..." ); |
098 | |
099 | String json = "{'database' : 'mkyongDB','table' : 'hosting'," + |
100 | "'detail' : {'records' : 99, 'index' : 'vps_index1', 'active' : 'true'}}}" ; |
101 | |
102 | DBObject dbObject = (DBObject)JSON.parse(json); |
103 | |
104 | collection.insert(dbObject); |
105 | |
106 | DBCursor cursorDocJSON = collection.find(); |
107 | while (cursorDocJSON.hasNext()) { |
108 | System.out.println(cursorDocJSON.next()); |
109 | } |
110 | |
111 | collection.remove( new BasicDBObject()); |
112 | |
113 | } catch (UnknownHostException e) { |
114 | e.printStackTrace(); |
115 | } catch (MongoException e) { |
116 | e.printStackTrace(); |
117 | } |
118 | |
119 | } |
120 | } |