一、下載jar包
http://central.maven.org/maven2/org/mongodb/mongo-java-driver/
二、代碼實現
1 package com.xbq.mongodb; 2 import java.util.ArrayList; 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6 import org.bson.types.ObjectId; 7 import com.mongodb.BasicDBObject; 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 /** 16 * @ClassName: MongoDBTest 17 * @Description: TODO MongoDB 增刪改查 操作,包含批量操作 18 * @author xbq 19 * @version 1.0 20 * @date 2017-4-5 上午11:50:06 21 */ 22 public class MongoDBTest { 23 24 private static final String HOST = "192.168.242.129"; 25 private static final int PORT = 27017; 26 private static final String DB_NAME = "testDB"; 27 private static Mongo mongo; 28 private static DB db; 29 30 static { 31 // 連接到MongoDB 32 mongo = new Mongo(HOST, PORT); 33 // 打開數據庫 testDB 34 db = mongo.getDB(DB_NAME); 35 } 36 37 public static void main(String[] args) { 38 // 獲取集合 xbqTable,若該集合不存在,mongoDB將自動創建該集合 39 DBCollection dbCollection = db.getCollection("testTable"); 40 41 // 查詢該數據庫所有的集合名 42 for(String name : mongo.getDatabaseNames()){ 43 System.out.println(name); 44 } 45 46 // addOne(dbCollection); 47 // addList(dbCollection); 48 // addByJson(dbCollection); 49 50 // deleteOne(dbCollection); 51 // deleteByIn(dbCollection); 52 // deleteAll(dbCollection); 53 54 // updateOne(dbCollection); 55 // updateMulti(dbCollection); 56 57 // queryOne(dbCollection); 58 // queryPage(dbCollection); 59 // queryRange(dbCollection); 60 queryList(dbCollection); 61 62 } 63 64 65 // ====================================查詢開始============================================== 66 /** 67 * @Title: queryOne 68 * @Description: TODO 查詢 name為 張三的 一條記錄 69 * @param dbCollection 70 * @return: void 71 */ 72 public static void queryOne(DBCollection dbCollection){ 73 DBObject documents = new BasicDBObject("name","張三"); 74 DBObject result = dbCollection.findOne(documents); 75 System.out.println(result); 76 } 77 78 /** 79 * @Title: queryPage 80 * @Description: TODO 分頁查詢 , 查詢 跳過前2條 后的 3條 數據 81 * @param dbCollection 82 * @return: void 83 */ 84 public static void queryPage(DBCollection dbCollection){ 85 DBCursor cursor = dbCollection.find().skip(2).limit(3); 86 while (cursor.hasNext()) { 87 System.out.println(cursor.next()); 88 } 89 } 90 91 /** 92 * @Title: queryRange 93 * @Description: TODO 范圍查詢,查詢 第3條 到 第5條 之間的記錄 94 * @param dbCollection 95 * @return: void 96 */ 97 public static void queryRange(DBCollection dbCollection) { 98 DBObject range = new BasicDBObject(); 99 range.put("$gte", 50); 100 range.put("$lte", 52); 101 102 DBObject dbObject = new BasicDBObject(); 103 dbObject.put("age", range); 104 DBCursor cursor = dbCollection.find(dbObject); 105 while (cursor.hasNext()) { 106 System.out.println(cursor.next()); 107 } 108 } 109 110 /**' 111 * @Title: queryList 112 * @Description: TODO 查詢出全部的 記錄 113 * @param dbCollection 114 * @return: void 115 */ 116 public static void queryList(DBCollection dbCollection) { 117 DBCursor cursor = dbCollection.find(); 118 DBObject dbObject = null; 119 while(cursor.hasNext()){ 120 dbObject = cursor.next(); 121 System.out.println(dbObject); 122 } 123 } 124 125 // ====================================增加開始============================================== 126 /** 127 * @Title: addOne 128 * @Description: TODO 新增 一條記錄 129 * @param dbCollection 130 * @return: void 131 */ 132 public static void addOne(DBCollection dbCollection){ 133 DBObject documents = new BasicDBObject("name","張三").append("age", 45).append("sex", "男").append("address", 134 new BasicDBObject("postCode", 100000).append("street", "深南大道888號").append("city", "深圳")); 135 dbCollection.insert(documents); 136 } 137 138 /** 139 * @Title: addList 140 * @Description: TODO 批量新增 記錄 , 增加的記錄 中 可以使用各種數據類型 141 * @param dbCollection 142 * @return: void 143 */ 144 public static void addList(DBCollection dbCollection){ 145 List<DBObject> listdbo= new ArrayList<DBObject>(); 146 DBObject dbObject = new BasicDBObject(); 147 dbObject.put("name", "老王"); 148 // 可以直接保存List類型 149 List<String> list = new ArrayList<String>(); 150 list.add("非隔壁老王"); 151 dbObject.put("remark", list); 152 listdbo.add(dbObject); 153 154 dbObject = new BasicDBObject(); 155 // 可以直接保存map 156 Map<String,List<String>> map = new HashMap<String,List<String>>(); 157 List<String> hobbys = new ArrayList<String>(); 158 hobbys.add("看花"); 159 hobbys.add("采花"); 160 map.put("愛好", hobbys); 161 dbObject.put("hobby", map); 162 listdbo.add(dbObject); 163 164 dbObject = new BasicDBObject(); 165 dbObject.put("name", "老張"); 166 dbObject.put("age", 52); 167 dbObject.put("job", "看守老王"); 168 dbObject.put("remark", new BasicDBObject("address", "廣東省深圳市").append("street", "深南大道888號")); 169 listdbo.add(dbObject); 170 171 dbCollection.insert(listdbo); 172 } 173 174 /** 175 * @Title: addByJson 176 * @Description: TODO json轉對象后 ,執行新增 177 * @param dbCollection 178 * @return: void 179 */ 180 public static void addByJson(DBCollection dbCollection){ 181 String json = "{ \"name\" : \"王五\" , \"age\" : 66 , \"job\" : \"看守老王\" , \"remark\" : { \"address\" : \"廣東省深圳市\" , \"street\" : \"深南大道888號\"}}"; 182 DBObject dbObject = (DBObject) JSON.parse(json); 183 dbCollection.insert(dbObject); 184 } 185 186 // ====================================修改開始============================================== 187 /** 188 * @Title: update 189 * @Description: TODO 修改指定記錄 190 * @param dbCollection 191 * @return: void 192 */ 193 public static void updateOne(DBCollection dbCollection) { 194 // 先根據id查詢將 這條 記錄查詢出來 195 DBObject qryResult = dbCollection.findOne(new ObjectId("58e4a11c6c166304f0635958")); 196 // 修改指定的值 197 qryResult.put("age", 55); 198 199 DBObject olddbObject = new BasicDBObject(); 200 olddbObject.put("_id", new ObjectId("58e4a11c6c166304f0635958")); 201 dbCollection.update(olddbObject, qryResult); 202 } 203 204 /** 205 * @Title: updateMulti 206 * @Description: TODO 修改 多條記錄 207 * @param dbCollection 208 * @return: void 209 */ 210 public static void updateMulti(DBCollection dbCollection) { 211 DBObject newdbObject = new BasicDBObject(); 212 newdbObject.put("name", "張三"); 213 newdbObject.put("address", "廣東深圳"); 214 newdbObject.put("remark", "張三是一個NB的Coder"); 215 216 DBObject olddbObject = new BasicDBObject(); 217 olddbObject.put("name", "張三"); 218 // 需要加上這個 219 DBObject upsertValue = new BasicDBObject("$set", newdbObject); 220 // 后面的兩個參數:1.若所更新的數據沒有,則插入 ; 2、同時更新多個符合條件的文檔(collection) 221 dbCollection.update(olddbObject, upsertValue, true, true); 222 } 223 224 // ====================================刪除開始============================================== 225 /** 226 * @Title: deleteFirst 227 * @Description: TODO 刪除第一個 228 * @param 229 * @return: void 230 */ 231 public static void deleteFirst(DBCollection dbCollection){ 232 DBObject dbObject = dbCollection.findOne(); 233 dbCollection.remove(dbObject); 234 } 235 236 /** 237 * @Title: deleteOne 238 * @Description: TODO 刪除指定的一條記錄 239 * @param dbCollection 240 * @return: void 241 */ 242 public static void deleteOne(DBCollection dbCollection){ 243 DBObject dbObject = new BasicDBObject(); 244 dbObject.put("_id", new ObjectId("58e49c2d6c166309e0d50484")); 245 dbCollection.remove(dbObject); 246 } 247 248 /** 249 * @Title: deleteByIn 250 * @Description: TODO 刪除多條記錄 例如:select * from tb where name in('12','34') 251 * @param dbCollection 252 * @return: void 253 */ 254 public static void deleteByIn(DBCollection dbCollection) { 255 List<String> list = new ArrayList<String>(); 256 list.add("老張"); 257 list.add("老王"); 258 list.add("張三"); 259 DBObject dbObject = new BasicDBObject("$in", list); 260 261 DBObject delObject = new BasicDBObject(); 262 delObject.put("name", dbObject); 263 dbCollection.remove(delObject); 264 } 265 266 /** 267 * @Title: deleteAll 268 * @Description: TODO 刪除全部的記錄 269 * @param dbCollection 270 * @return: void 271 */ 272 public static void deleteAll(DBCollection dbCollection){ 273 DBCursor cursor = dbCollection.find(); 274 while(cursor.hasNext()){ 275 dbCollection.remove(cursor.next()); 276 } 277 } 278 }
三、源碼下載
