mogoDB 4.2.0安装部署及JAVA 客戶端应用
1、服务安装与卸载
cd C:\Program Files\MongoDB\Server\4.2\bin
安装服务:mongod --config "C:/Program Files/MongoDB/Server/4.2/bin/mongod.cfg" --dbpath="D:\MongoDB\data\db" --logpath="D:\MongoDB\data\logs\MongoDB.log" --install --serviceName "MongoDB"
卸载服务:mongod.exe --remove --serviceName "MongoDB"
2、创建数据库及用户
mogo
use admin
db.createUser({user:'myAdmin', pwd:'myAdmin111', roles:[{role:'root',db:'admin'}]})
db.auth('myAdmin','myAdmin111')
use myDB
db.createUser({user:"user1",pwd:"123",roles:[{ role: "readWrite", db: "myDB" }]})
db.grantRolesToUser("user1", [ { role:"dbAdmin", db: "myDB"} ])
db.grantRolesToUser("user1", [ { role:"readWrite", db: "myDB"} ])
MongoDB 目前内置了 7 个角色。
数据库用户角色:read、readWrite;
数据库管理角色:dbAdmin、dbOwner、userAdmin;
集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
备份恢复角色:backup、restore;
所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
超级用户角色:root;这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
内部角色:__system
这些角色对应的作用如下:
Read:允许用户读取指定数据库
readWrite:允许用户读写指定数据库
dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
root:只在admin数据库中可用。超级账号,超级权限
3、验证用户
mongo 192.168.1.230:27017/myDB -u "user1" -p "123"
db.auth('user1','123')
db.collection1.insert({title: 'MongoDB 教程'})
4、mongod.cfg 配置
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: D:\MongoDB\Server\4.2\data journal: enabled: true # engine: # mmapv1: # wiredTiger: # where to write logging data. systemLog: destination: file logAppend: true path: D:\MongoDB\Server\4.2\log\mongod.log # network interfaces net: port: 27017 bindIp: 0.0.0.0 #processManagement: #security: # authorization: enabled #operationProfiling: #replication: #sharding: ## Enterprise-Only Options: #auditLog: #snmp:
5、数据库管理工具:robmongo
6、JAVA操作
1 package com.xrh.core.db; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.Set; 8 import java.util.logging.Logger; 9 import java.util.regex.Pattern; 10 11 import org.bson.Document; 12 import org.bson.conversions.Bson; 13 import org.bson.types.ObjectId; 14 15 import com.mongodb.MongoClientSettings; 16 import com.mongodb.MongoCredential; 17 import com.mongodb.ServerAddress; 18 import com.mongodb.client.FindIterable; 19 import com.mongodb.client.MongoClient; 20 import com.mongodb.client.MongoClients; 21 import com.mongodb.client.MongoCollection; 22 import com.mongodb.client.MongoCursor; 23 import com.mongodb.client.MongoDatabase; 24 import com.mongodb.client.model.Filters; 25 import com.mongodb.client.result.DeleteResult; 26 import com.mongodb.client.result.UpdateResult; 27 import com.xrh.base.config.DAO_Config; 28 import com.xrh.core.BN_Page; 29 import com.xrh.core.BN_Row; 30 import com.xrh.core.util.JsonUtil; 31 import com.xrh.core.util.ObjectUtil; 32 33 /** 34 * mongoDB 数据持久层处理类 35 * @author Li XiaoJia 36 * 37 */ 38 public class MongoRunner { 39 40 public static Logger logger = Logger.getLogger(MongoRunner.class.getName()); 41 private static MongoClient mongoClient = null; 42 private static String userName = DAO_Config.getConfigValue("mongo-userName"); 43 private static String password = DAO_Config.getConfigValue("mongo-password"); 44 private static String serverAddress = DAO_Config.getConfigValue("mongo-serverAddress");//多个用,分隔 45 private MongoRunner(){} 46 47 /** 48 * 获取客户端链接实例 49 * @return MongoClient 50 */ 51 public static MongoClient getMongoClient(){ 52 if ( mongoClient == null ){ 53 String database = DAO_Config.getConfigValue("mongo-database"); 54 MongoCredential credential = MongoCredential.createCredential(userName, database, password.toCharArray()); 55 List<ServerAddress> servers = new ArrayList(); 56 logger.info("serverAddress="+serverAddress); 57 String[] serverAddressArr = serverAddress.split(","); 58 for (String serverAddressStr : serverAddressArr){ 59 if (serverAddressStr.contains(":")){ 60 servers.add(new ServerAddress(serverAddressStr.split(":")[0] 61 ,Integer.parseInt(serverAddressStr.split(":")[1]))); 62 }else{ 63 servers.add(new ServerAddress(serverAddressStr)); 64 } 65 } 66 MongoClientSettings settings = MongoClientSettings.builder() 67 .credential(credential) 68 // .applyToSslSettings(builder -> builder.enabled(true)) 69 .applyToClusterSettings(builder -> 70 builder.hosts(servers)) 71 .build(); 72 mongoClient = MongoClients.create(settings); 73 return mongoClient; 74 } 75 return mongoClient; 76 } 77 78 private static MongoDatabase getDatabase(String database){ 79 if (ObjectUtil.isNull(database)){ 80 database = DAO_Config.getConfigValue("mongo-database"); 81 } 82 return getMongoClient().getDatabase(database); 83 } 84 85 /** 86 * 关闭客户端链接 87 */ 88 public static void closeMongoClient(){ 89 if ( mongoClient != null ){ 90 logger.info("关闭mongoClient."); 91 mongoClient.close(); 92 mongoClient = null; 93 } 94 } 95 96 /** 97 * 添加一条记录 98 * @param table 表名 99 * @param row BN_Row行对象数据 100 */ 101 public static void insertRow(String table, BN_Row row) { 102 if (ObjectUtil.isNull(table)){ 103 logger.warning("table 参数不能为空!"); 104 return ; 105 } 106 if (row == null){ 107 logger.warning("row 参数对象不能为空!"); 108 return ; 109 } 110 String database = DAO_Config.getConfigValue("mongo-database"); 111 insertRow(database, table, row); 112 } 113 114 115 116 /** 117 * 添加一条记录 118 * @param database 数据库名 119 * @param table 表名 120 * @param row BN_Row行对象数据 121 */ 122 public static void insertRow(String database, String table, BN_Row row) { 123 if (ObjectUtil.isNull(database)){ 124 logger.warning("database 参数不能为空!"); 125 return ; 126 } 127 if (ObjectUtil.isNull(table)){ 128 logger.warning("table 参数不能为空!"); 129 return ; 130 } 131 if (row == null){ 132 logger.warning("row 参数对象不能为空!"); 133 return ; 134 } 135 MongoDatabase db = getDatabase(database); 136 MongoCollection<Document> collection = db.getCollection(table); 137 Document document = new Document(); 138 document.putAll(row.columnMap); 139 logger.info("insertRow document="+document.toJson()); 140 collection.insertOne(document); 141 } 142 143 /** 144 * 添加一条记录 145 * @param table 表名 146 * @param row map行对象数据 147 */ 148 public static void insertRow(String table, Map row) { 149 if (ObjectUtil.isNull(table)){ 150 logger.warning("table 参数不能为空!"); 151 return ; 152 } 153 if (row == null){ 154 logger.warning("row 参数Map对象不能为空!"); 155 return ; 156 } 157 String database = DAO_Config.getConfigValue("mongo-database"); 158 insertRow(database, table, row); 159 } 160 161 /** 162 * 添加一条记录 163 * @param database 数据库名 164 * @param table 表名 165 * @param row map行对象数据 166 */ 167 public static void insertRow(String database, String table, Map row) { 168 if (ObjectUtil.isNull(database)){ 169 logger.warning("database 参数不能为空!"); 170 return ; 171 } 172 if (ObjectUtil.isNull(table)){ 173 logger.warning("table 参数不能为空!"); 174 return ; 175 } 176 if (row == null){ 177 logger.warning("row 参数Map对象不能为空!"); 178 return ; 179 } 180 MongoDatabase db = getDatabase(database); 181 MongoCollection<Document> collection = db.getCollection(table); 182 Document document = new Document(); 183 document.putAll(row); 184 logger.info("insertRow document="+document.toJson()); 185 collection.insertOne(document); 186 } 187 188 /** 189 * 批量添加数据 190 * @param table 表名 191 * @param rowList 多条数据 192 */ 193 public static void insertList(String table, List<BN_Row> rowList) { 194 if (ObjectUtil.isNull(table)){ 195 logger.warning("table 参数不能为空!"); 196 return ; 197 } 198 if (rowList == null){ 199 logger.warning("rowList 参数List<BN_Row>对象不能为空!"); 200 return ; 201 } 202 String database = DAO_Config.getConfigValue("mongo-database"); 203 insertList(database, table, rowList); 204 } 205 206 /** 207 * 批量添加数据 208 * @param database 数据库名 209 * @param table 表名 210 * @param rowList 多条数据 211 */ 212 public static void insertList(String database, String table, List<BN_Row> rowList) { 213 if (ObjectUtil.isNull(database)){ 214 logger.warning("database 参数不能为空!"); 215 return ; 216 } 217 if (ObjectUtil.isNull(table)){ 218 logger.warning("table 参数不能为空!"); 219 return ; 220 } 221 if (rowList == null){ 222 logger.warning("rowList 参数List<BN_Row>对象不能为空!"); 223 return ; 224 } 225 MongoDatabase db = getDatabase(database); 226 MongoCollection<Document> collection = db.getCollection(table); 227 List<Document> list = new ArrayList(); 228 for (BN_Row row : rowList){ 229 Document document = new Document(); 230 document.putAll(row.columnMap); 231 list.add(document); 232 logger.info("insertList document="+document.toJson()); 233 } 234 collection.insertMany(list); 235 } 236 237 /** 238 * 更新 239 * @param table 240 * @param row 241 * @return 242 */ 243 public static long updateRow(String table, BN_Row row) { 244 if (ObjectUtil.isNull(table)){ 245 logger.warning("table 参数不能为空!"); 246 return -1; 247 } 248 if (row == null){ 249 logger.warning("row 参数BN_Row对象不能为空!"); 250 return -1; 251 } 252 String database = DAO_Config.getConfigValue("mongo-database"); 253 return updateRow(database, table, row); 254 } 255 256 /** 257 * 更新 258 * @param database 数据库名 259 * @param table 260 * @param row 261 * @return 262 */ 263 public static long updateRow(String database, String table, BN_Row row) { 264 if (ObjectUtil.isNull(database)){ 265 logger.warning("database 参数不能为空!"); 266 return -1; 267 } 268 if (ObjectUtil.isNull(table)){ 269 logger.warning("table 参数不能为空!"); 270 return -1; 271 } 272 if (row == null){ 273 logger.warning("row 参数BN_Row对象不能为空!"); 274 return -1; 275 } 276 MongoDatabase db = getDatabase(database); 277 MongoCollection<Document> collection = db.getCollection(table); 278 String keyValue = row.columnMap.get("id").toString(); 279 Document document = new Document(); 280 row.removeColumn("id"); 281 document.putAll(row.columnMap); 282 logger.info("updateRow _id=" + keyValue + " document="+document.toJson()); 283 Bson filter = Filters.eq("_id", new ObjectId(keyValue)); 284 UpdateResult updateResult = collection.updateOne(filter, 285 new Document("$set", document)); 286 return updateResult.getModifiedCount(); 287 } 288 289 /** 290 * 删除数据 291 * @param table 表名 292 * @param _id _id值 293 * @return 294 */ 295 public static long deleteRow(String table, String id) { 296 if (ObjectUtil.isNull(table)){ 297 logger.warning("table 参数不能为空!"); 298 return -1; 299 } 300 if (ObjectUtil.isNull(id)){ 301 logger.warning("id 参数不能为空!"); 302 return -1; 303 } 304 String database = DAO_Config.getConfigValue("mongo-database"); 305 return deleteRow(database, table, id); 306 } 307 308 /** 309 * 删除数据 310 * @param database 数据库名 311 * @param table 表名 312 * @param _id _id值 313 * @return 314 */ 315 public static long deleteRow(String database, String table, String id) { 316 if (ObjectUtil.isNull(database)){ 317 logger.warning("database 参数不能为空!"); 318 return -1; 319 } 320 if (ObjectUtil.isNull(table)){ 321 logger.warning("table 参数不能为空!"); 322 return -1; 323 } 324 if (ObjectUtil.isNull(id)){ 325 logger.warning("id 参数不能为空!"); 326 return -1; 327 } 328 MongoDatabase db = getDatabase(database); 329 MongoCollection<Document> collection = db.getCollection(table); 330 logger.info("deleteRow _id=" + id); 331 DeleteResult deleteResult = collection.deleteOne(Filters.eq("_id", new ObjectId(id))); 332 return deleteResult.getDeletedCount(); 333 } 334 335 /** 336 * 删除符合条件的数据表记录 337 * @param table 338 * @param queryJson eg:"{"_id":"=@5e4f9f205291a2268e5576b8","osName":"LIKE@测试"}" 339 * @return 340 */ 341 public static long deleteMany(String table, String queryJson) { 342 if (ObjectUtil.isNull(table)){ 343 logger.warning("table 参数不能为空!"); 344 return -1; 345 } 346 if (ObjectUtil.isNull(queryJson)){ 347 logger.warning("queryJson 参数不能为空!"); 348 return -1; 349 } 350 String database = DAO_Config.getConfigValue("mongo-database"); 351 return deleteMany(database, table, queryJson); 352 } 353 354 /** 355 * 删除符合条件的数据表记录 356 * @param database 357 * @param table 358 * @param queryJson eg:"{"_id":"=@5e4f9f205291a2268e5576b8","osName":"LIKE@测试"}" 359 * @return 360 */ 361 public static long deleteMany(String database, String table, String queryJson) { 362 if (ObjectUtil.isNull(database)){ 363 logger.warning("database 参数不能为空!"); 364 return -1; 365 } 366 if (ObjectUtil.isNull(table)){ 367 logger.warning("table 参数不能为空!"); 368 return -1; 369 } 370 if (ObjectUtil.isNull(queryJson)){ 371 logger.warning("queryJson 参数不能为空!"); 372 return -1; 373 } 374 MongoDatabase db = getDatabase(database); 375 MongoCollection<Document> collection = db.getCollection(table); 376 List<Bson> queryObject = queryBsonHandler(queryJson); 377 logger.info("deleteMany queryJson=" + queryJson); 378 DeleteResult deleteResult = collection.deleteMany(Filters.and(queryObject)); 379 return deleteResult.getDeletedCount(); 380 } 381 382 /** 383 * 删除指定表的所有数据 384 * @param table 385 * @return 386 */ 387 public static boolean deleteAll(String table) { 388 if (ObjectUtil.isNull(table)){ 389 logger.warning("table 参数不能为空!"); 390 return false; 391 } 392 String database = DAO_Config.getConfigValue("mongo-database"); 393 return deleteAll(database, table); 394 } 395 396 /** 397 * 删除指定表的所有数据 398 * @param table 399 * @return 400 */ 401 public static boolean deleteAll(String database, String table) { 402 if (ObjectUtil.isNull(database)){ 403 logger.warning("database 参数不能为空!"); 404 return false; 405 } 406 if (ObjectUtil.isNull(table)){ 407 logger.warning("table 参数不能为空!"); 408 return false; 409 } 410 logger.info("deleteAll table=" + table); 411 MongoDatabase db = getDatabase(database); 412 MongoCollection<Document> collection = db.getCollection(table); 413 collection.drop(); 414 return true; 415 } 416 417 /** 418 * 查询单条记录 419 * @param table 420 * @param _id 421 * @param beanClass 422 * @return 423 */ 424 public static BN_Row selectRow(String table, String id, Class beanClass){ 425 if (ObjectUtil.isNull(table)){ 426 logger.warning("table 参数不能为空!"); 427 return null; 428 } 429 if (ObjectUtil.isNull(id)){ 430 logger.warning("id 参数不能为空!"); 431 return null; 432 } 433 if (beanClass == null){ 434 logger.warning("beanClass 参数Class对象不能为空!"); 435 return null; 436 } 437 String database = DAO_Config.getConfigValue("mongo-database"); 438 return selectRow(database, table, id, beanClass); 439 } 440 441 /** 442 * 查询单条记录 443 * @param database 444 * @param table 445 * @param _id 446 * @param beanClass 447 * @return 448 */ 449 public static BN_Row selectRow(String database, String table, String id, Class beanClass){ 450 if (ObjectUtil.isNull(database)){ 451 logger.warning("database 参数不能为空!"); 452 return null; 453 } 454 if (ObjectUtil.isNull(table)){ 455 logger.warning("table 参数不能为空!"); 456 return null; 457 } 458 if (ObjectUtil.isNull(id)){ 459 logger.warning("id 参数不能为空!"); 460 return null; 461 } 462 if (beanClass == null){ 463 logger.warning("beanClass 参数Class对象不能为空!"); 464 return null; 465 } 466 MongoDatabase db = getDatabase(database); 467 MongoCollection<Document> collection = db.getCollection(table); 468 String json = collection.find(Filters.eq("_id", new ObjectId(id))).first().toJson(); 469 logger.info("selectRow result=" + json); 470 Map map = JsonUtil.json2Obj(json, Map.class); 471 BN_Row row = null; 472 try { 473 row = (BN_Row) beanClass.newInstance(); 474 } catch (InstantiationException e) { 475 // TODO Auto-generated catch block 476 e.printStackTrace(); 477 } catch (IllegalAccessException e) { 478 // TODO Auto-generated catch block 479 e.printStackTrace(); 480 } 481 map.remove("_id"); 482 row.columnMap.putAll(map); 483 row.columnMap.put("id", id); 484 return row; 485 } 486 487 /** 488 * 查询符合条件的多条记录 489 * @param table 490 * @param queryJson eg:{\"osName\":\"=@微软操作系统1\"} 491 * @param sortColumn 492 * @param order 493 * @param limit 494 * @param beanClass 495 * @return 496 */ 497 public static List<BN_Row> selectList(String table, String queryJson, String sortColumn, 498 String order, int limit, Class beanClass){ 499 if (ObjectUtil.isNull(table)){ 500 logger.warning("table 参数不能为空!"); 501 return null; 502 } 503 if (beanClass == null){ 504 logger.warning("beanClass 参数Class对象不能为空!"); 505 return null; 506 } 507 String database = DAO_Config.getConfigValue("mongo-database"); 508 return selectList(database, table, queryJson, sortColumn, order, limit, beanClass); 509 } 510 511 /** 512 * 查询符合条件的多条记录 513 * @param database 514 * @param table 515 * @param queryJson 516 * @param sortColumn 517 * @param order 518 * @param limit 519 * @param beanClass 520 * @return 521 */ 522 public static List<BN_Row> selectList(String database, String table, String queryJson, String sortColumn, 523 String order, int limit, Class beanClass){ 524 if (ObjectUtil.isNull(database)){ 525 logger.warning("database 参数不能为空!"); 526 return null; 527 } 528 if (ObjectUtil.isNull(table)){ 529 logger.warning("table 参数不能为空!"); 530 return null; 531 } 532 if (beanClass == null){ 533 logger.warning("beanClass 参数Class对象不能为空!"); 534 return null; 535 } 536 MongoDatabase db = getDatabase(database); 537 MongoCollection<Document> collection = db.getCollection(table); 538 List<Bson> queryObject = queryBsonHandler(queryJson); 539 logger.info("queryObject="+queryObject.toString()); 540 541 FindIterable<Document> findIterable = null; 542 if ( queryObject.size() == 0 ){ 543 findIterable = collection.find(); 544 } else { 545 findIterable = collection.find(Filters.and(queryObject)); 546 } 547 if (!ObjectUtil.isNull(sortColumn) 548 && !ObjectUtil.isNull(order) ){ 549 Bson sort = Filters.eq(sortColumn, order.equals("asc")? 1 : -1); 550 findIterable = findIterable.sort(sort); 551 } 552 553 if (limit > 0){ 554 findIterable = findIterable.limit(limit); 555 } 556 MongoCursor<Document> iterator = null; 557 List results = new ArrayList(); 558 try { 559 iterator = findIterable.iterator(); 560 while (iterator.hasNext()){ 561 Document document = iterator.next(); 562 ObjectId objectId = document.getObjectId("_id"); 563 BN_Row row = (BN_Row) beanClass.newInstance(); 564 Map map = JsonUtil.json2Obj(document.toJson(), Map.class); 565 map.remove("_id"); 566 row.columnMap.putAll(map); 567 row.columnMap.put("id", objectId.toString()); 568 results.add(row); 569 } 570 } catch (InstantiationException e) { 571 // TODO Auto-generated catch block 572 e.printStackTrace(); 573 } catch (IllegalAccessException e) { 574 // TODO Auto-generated catch block 575 e.printStackTrace(); 576 } finally { 577 if ( iterator != null ){ 578 iterator.close(); 579 } 580 } 581 return results; 582 } 583 584 /** 585 * 查询翻页数据 586 * @param page 587 * @param beanClass 588 * @return 589 */ 590 public static BN_Page selectPage(BN_Page page, Class beanClass){ 591 if (page == null){ 592 logger.warning("page 参数BN_Page不能为空!"); 593 return null; 594 } 595 if (beanClass == null){ 596 logger.warning("beanClass 参数Class不能为空!"); 597 return null; 598 } 599 String database = DAO_Config.getConfigValue("mongo-database"); 600 return selectPage(database, page, beanClass); 601 } 602 603 /** 604 * 查询翻页数据 605 * @param database 606 * @param page 607 * @param beanClass 608 * @return 609 */ 610 public static BN_Page selectPage(String database, BN_Page page, Class beanClass){ 611 if (ObjectUtil.isNull(database)){ 612 logger.warning("database 参数不能为空!"); 613 return null; 614 } 615 if (page == null){ 616 logger.warning("page 参数不能为空!"); 617 return null; 618 } 619 if (beanClass == null){ 620 logger.warning("beanClass 参数Class对象不能为空!"); 621 return null; 622 } 623 MongoDatabase db = getDatabase(database); 624 MongoCollection<Document> collection = db.getCollection(page.getTableName()); 625 List<Bson> queryObject = queryBsonHandler(page.getWhereCond()); 626 627 logger.info("collection="+page.getTableName()); 628 logger.info("queryObject="+queryObject.toString()); 629 logger.info("sort="+page.getSort()); 630 logger.info("order="+page.getOrder()); 631 logger.info("limit="+page.getRows()); 632 logger.info("skip="+page.getRowNo()); 633 634 Bson sort = Filters.eq(page.getSort(), page.getOrder().equals("asc")? 1 : -1); 635 FindIterable<Document> findIterable = null; 636 if ( queryObject.size() == 0 ){ 637 findIterable = collection.find(); 638 page.setRowCount(collection.countDocuments()); 639 } else { 640 findIterable = collection.find(Filters.and(queryObject)); 641 page.setRowCount(collection.countDocuments(Filters.and(queryObject))); 642 } 643 findIterable = findIterable.sort(sort) 644 .limit(page.getRows()) 645 .skip(page.getRowNo()); 646 MongoCursor<Document> iterator = null; 647 try { 648 iterator = findIterable.iterator(); 649 while (iterator.hasNext()){ 650 Document document = iterator.next(); 651 ObjectId objectId = document.getObjectId("_id"); 652 BN_Row row = (BN_Row) beanClass.newInstance(); 653 Map map = JsonUtil.json2Obj(document.toJson(), Map.class); 654 map.remove("_id"); 655 row.columnMap.putAll(map); 656 row.columnMap.put("id", objectId.toString()); 657 page.addRow(row); 658 } 659 } catch (InstantiationException e) { 660 // TODO Auto-generated catch block 661 e.printStackTrace(); 662 } catch (IllegalAccessException e) { 663 // TODO Auto-generated catch block 664 e.printStackTrace(); 665 } finally { 666 if ( iterator != null ){ 667 iterator.close(); 668 } 669 } 670 return page; 671 } 672 673 /** 674 * 转换为List<Bson>查询对象 675 * @param queryJson 676 * @return 677 */ 678 private static List<Bson> queryBsonHandler(String queryJson){ 679 List<Bson> querys = new ArrayList(); 680 if (!ObjectUtil.isNull(queryJson)){ 681 logger.info("queryBsonHandler whereCond="+queryJson); 682 List<Map<String, Object>> jsonList = JsonUtil.json2List("["+queryJson+"]"); 683 Map<String, Object> map = jsonList.get(0); 684 Set<String> set = map.keySet(); 685 for (Iterator<String> it = set.iterator();it.hasNext();) { 686 String queryColumn = it.next(); 687 String queryValue = map.get(queryColumn).toString(); 688 if (queryColumn.startsWith("B$_") || queryColumn.startsWith("E$_")){ 689 queryColumn = queryColumn.substring(3); 690 } 691 if (queryValue.startsWith("LIKE@")){ 692 querys.add(Filters.regex(queryColumn, 693 Pattern.compile("^.*"+queryValue.substring(5)+".*$", Pattern.CASE_INSENSITIVE))); 694 }else if (queryValue.startsWith("=@")){ 695 if ("id".equals(queryColumn)){ 696 querys.add(Filters.eq("_id", new ObjectId(queryValue.substring(2)))); 697 }else{ 698 querys.add(Filters.eq(queryColumn, queryValue.substring(2))); 699 } 700 }else if (queryValue.startsWith("=INT@")){ 701 querys.add(Filters.eq(queryColumn, new Long(queryValue.substring(5)).longValue())); 702 }else if (queryValue.startsWith(">@")){ 703 querys.add(Filters.gt(queryColumn, queryValue.substring(2))); 704 }else if (queryValue.startsWith(">INT@")){ 705 querys.add(Filters.gt(queryColumn, new Integer(queryValue.substring(5)).intValue())); 706 }else if (queryValue.startsWith(">=@")){ 707 querys.add(Filters.gte(queryColumn, queryValue.substring(3))); 708 }else if (queryValue.startsWith(">=INT@")){ 709 querys.add(Filters.gte(queryColumn, new Integer(queryValue.substring(6)).intValue())); 710 }else if (queryValue.startsWith("<@")){ 711 querys.add(Filters.lt(queryColumn, queryValue.substring(2))); 712 }else if (queryValue.startsWith("<INT@")){ 713 querys.add(Filters.lt(queryColumn, new Integer(queryValue.substring(5)).intValue())); 714 }else if (queryValue.startsWith("<=@")){ 715 querys.add(Filters.lte(queryColumn, queryValue.substring(3))); 716 }else if (queryValue.startsWith("<=INT@")){ 717 querys.add(Filters.lte(queryColumn, new Integer(queryValue.substring(6)).intValue())); 718 }else if (queryValue.startsWith("!=@")){ 719 querys.add(Filters.ne(queryColumn, queryValue.substring(3))); 720 }else if (queryValue.startsWith("!=INT@")){ 721 querys.add(Filters.ne(queryColumn, new Integer(queryValue.substring(6)).intValue())); 722 } 723 } 724 } 725 return querys; 726 } 727 728 public static void main(String[] args){ 729 730 } 731 732 }