MongoDB(二)
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>
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 }
页:
[1]