系列导航
数据库连接通用类参考:三、java连接mongo数据库
操作mongo的方法有很多种,如下是我比较习惯的写法,查询是最复杂的请大家参考。
一、mongo新增
//把json当做数据内容插入mongo库中 public int insertTrack(JSONObject jsonObject) { int result = 0; MongoDatabase database = MongoDbDao.getDb("testDb"); MongoCollection picContent = database.getCollection("traceTable", BasicDBObject.class); DBObject dbObject = (DBObject) JSON.parse(jsonObject.toString()); picContent.insertOne(dbObject); return result; }
二、mongo删除
public int removeTrack(String trackId) { int result = 0; MongoDatabase database = MongoDbDao.getDb("testDb"); MongoCollection picContent = database.getCollection("traceTable", BasicDBObject.class); //如下注意一般mongo自带的主键如右边 "_id" : ObjectId("60703e8f7b85766cd2554cf9") 字符串外层会带一个ObjectId()操作_id的时候使用new ObjectId()就可以 BasicDBObject delSql = new BasicDBObject("_id", new ObjectId(trackId)); picContent.findOneAndDelete(delSql); return result; }
三、mongo更新
public void updateTrack() { MongoDatabase database = MongoDbDao.getDb("etc_data"); MongoCollection picContent = database.getCollection("new_trace", BasicDBObject.class); BasicDBObject whereSql = new BasicDBObject("_id", new ObjectId("6124b4c72b2ea593ccdd3e60")); BasicDBObject updateNewOneSql = new BasicDBObject("$set", new BasicDBObject("address", "11111")); picContent.updateOne(whereSql,updateNewOneSql) ; }
四、mongo查询
//综合的查询方法包括如何设置查询条件、返回的数据项、排序规则,返回的数据条数 private void selectTrack() { MongoDatabase database = MongoDbDao.getDb("testDb"); //ReadPreference.secondaryPreferred() 意思为在集群模式下查寻从库, MongoCollection<BasicDBObject> collection = database.getCollection("oiltrack", BasicDBObject.class).withReadPreference(ReadPreference.secondaryPreferred()); String trackId = "123"; String idNum = "123456"; Timestamp createDateTime = getTimestamp(); //设置查询条件 BasicDBObject idCondObj = null; BasicDBList idBasicDBList = new BasicDBList(); idBasicDBList.add(new BasicDBObject("idNum", idNum)); //相等条件 idBasicDBList.add(new BasicDBObject("trackId", new BasicDBObject("$ne", trackId)));//不相等条件 idBasicDBList.add(new BasicDBObject("createDateTime", new BasicDBObject("$gt", new Date(createDateTime.getTime() - 24 * 60 * 60 * 1000))));//时间大于使用 idBasicDBList.add(new BasicDBObject("createDateTime", new BasicDBObject("$lt", new Date(createDateTime.getTime()))));//时间小于使用 idCondObj = new BasicDBObject().append(QueryOperators.AND, idBasicDBList); //查询条件 //设置返回字段列表 BasicDBObject selectObj = new BasicDBObject("colum1", 1).append("colum2", 1).append("colum3", 1); //设置排序字段 BasicDBObject sortObj = new BasicDBObject(); sortObj.put("dateTime", 1); List<String> trackBeanTrackIdList = new ArrayList<String>(); ArrayList<BasicDBObject> idRetList = new ArrayList<BasicDBObject>(); //limit(10)只查询前十条,不需要可以去掉 collection.find(idCondObj).projection(selectObj).sort(sortObj).limit(10).into(idRetList); for (BasicDBObject tempValue : idRetList) { System.out.println(tempValue.getString("colum1")); System.out.println(tempValue.getString("colum2")); } } //查询mongo记录中某属性字段不存在的记录 private void selectExistsTrack() { MongoDatabase database = MongoDbDao.getDb("testDb"); //ReadPreference.secondaryPreferred() 意思为在集群模式下查寻从库, MongoCollection<BasicDBObject> collection = database.getCollection("oiltrack", BasicDBObject.class).withReadPreference(ReadPreference.secondaryPreferred()); //需要查询的字段 Map valueMap = new HashMap(); valueMap.put("_id", 1); valueMap.put("name", 1); BasicDBObject whereObj = new BasicDBObject(); //查询记录中没有date这个属性的记录 whereObj.put("date", new BasicDBObject("$exists", false)); //batchSize(100)很重要,如果数据量特别大不加这个执行中会报错,batchSize(100)意思是每次取出100条数据,循环结束再取100条 Iterator iterator = collection.find(whereObj).batchSize(100).projection(new BasicDBObject(valueMap)).iterator(); while (iterator.hasNext()) { BasicDBObject obj = (BasicDBObject) iterator.next(); String _id = obj.getString("_id"); System.out.println("_id:" + _id); } } //获取当前时间 public Timestamp getTimestamp() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(currentTime); Timestamp timestamp = Timestamp.valueOf(dateString); return timestamp; }