MongoDB连接:
public class MongoDBOperate { private static final Logger logger = LogUtils.getLogger(); private static DruidDataSource dataSource; public static MongoClient client = null; public static String Ip = null; public static int Port = 0; public static String DatabaseName =null; public static String UserName =null; public static String Pwd =null; static { Properties dbProperties = new Properties(); for (String name : PropertiesReader.getPropertyNames()) { if (name != null) { if (name.equals("md_password")) { dbProperties.put(name.replace("md_", ""), PropertiesReader.getProperty(name)); Pwd = PropertiesReader.getProperty(name); } if (name.equals("md_userName")) { dbProperties.put(name.replace("md_", ""), PropertiesReader.getProperty(name)); UserName = PropertiesReader.getProperty(name); } if (name.equals("md_databaseName")) { dbProperties.put(name.replace("md_", ""), PropertiesReader.getProperty(name)); DatabaseName = PropertiesReader.getProperty(name); } if (name.equals("md_port")) { dbProperties.put(name.replace("md_", ""), PropertiesReader.getProperty(name)); Port = Integer.parseInt(PropertiesReader.getProperty(name)); } if (name.equals("md_host")) { dbProperties.put(name.replace("md_", ""), PropertiesReader.getProperty(name)); Ip = PropertiesReader.getProperty(name); } } } /** * 获取连接 * @return */ public MongoClient getCon(){ if(client==null){ client = new MongoClient(Ip,Port, DatabaseName,UserName,Pwd); } return client; } }
MongoDB操作:
public class MongoDbOperate { /** * 查询前一天所有上传数据 * * @return */ public static List<CpyGateway> queryAll(Long stime, Long etime) { //获取连接 MongoDBOperate mongoDBOperate = new MongoDBOperate(); MongoClient client = mongoDBOperate.getCon(); List<CpyGateway> list = new ArrayList<>(); String companycode = null; String waycode = null; AggregateIterable<Document> groupResult = client.getCollection("data_Collection_Log").aggregate(Arrays.asList( // match中写查询条件 Aggregates.match( Filters.and( Filters.lte("c_time", etime), Filters.gte("c_time", stime) ) ), Aggregates.group("$gatewayId", // Accumulators.sum("data","") Accumulators.sum("_id", "$sum:1"), Accumulators.addToSet("gatewayId", "$gatewayId") ) )); for (Document document : groupResult) { com.alibaba.fastjson.JSONObject jo = JSON.parseObject(document.toJson()); com.alibaba.fastjson.JSONArray gatewayArray = JSON.parseArray(jo.getString("gatewayId")); for (int i = 0; i < gatewayArray.size(); i++) { CpyGateway cg = new CpyGateway(); waycode = gatewayArray.get(i).toString(); companycode = waycode.substring(0, 8); cg.setCompanycode(companycode); cg.setWaycode(waycode); list.add(cg); } } return list; } }
AggregateIterable<Document> groupResult = collection.aggregate(Arrays.asList( // match中写查询条件 Aggregates.match( Filters.and( Filters.lte("date", 1532679880386L), Filters.gte("date", 1532677122452L)) ), // sort写排序条件,value为1是正序,-1为倒序,append用来控制多列排序 Aggregates.sort(new Document("date", -1).append("name", 1)), // group中写聚合操作,id为聚合键,可写MongoDB表达式; // 后续的参数为分组后的列,可使用Accumulators类中的静态方法进一步取第一条/最后一条、最大/最小值、求和、求平均值等操作 Aggregates.group("$name", Accumulators.first("date", "$date"), Accumulators.first("children", "$children")) )); for (Document document : groupResult) { System.out.println(document.toJson()); }