时间:2021-07-01 10:21:17 帮助过:16人阅读
二、使用操作,以下是查询的例子
public List<Data> getAllAbsData(RankParam rankParam) { List<Data> dataList = new ArrayList<Data>(); MongoDatabase mongoDatabase = MongoDbConn.getDatabase("localhost", 27017, "area_new"); MongoCollection<Document> collection = mongoDatabase.getCollection("vaabs_monthly"); FindIterable<Document> findIterable = null; if ("vaabs".equals(rankParam.getIndexCode())) { if (rankParam.getAreaCode() == null || "".equals(rankParam.getAreaCode())) {// 全国 //db.vaabs_monthly.find({"$and":[{"indusId":300},{"areaCode":/.*0000$/}]}) Pattern queryPattern = Pattern.compile(".*0000$", Pattern.CASE_INSENSITIVE); BasicDBObject queryObject = new BasicDBObject(); queryObject.put("areaCode", queryPattern); queryObject.put("indusId", rankParam.getIndusId()); findIterable = collection.find(queryObject); }else {//全省所有市的数据 //db.vaabs_monthly.find({"$and":[{"indusId":300},{"date":"2007-01-31"},{"areaCode":/^13/},{"areaCode":/.*00$/},{"areaCode":{"$not":/.*0000$/}}]}) //Pattern startPattern = Pattern.compile("^13", Pattern.CASE_INSENSITIVE); Pattern startPattern = Pattern.compile("^"+rankParam.getAreaCode(), Pattern.CASE_INSENSITIVE); Pattern endPattern = Pattern.compile(".*00$", Pattern.CASE_INSENSITIVE); Pattern notPattern = Pattern.compile(".*0000$", Pattern.CASE_INSENSITIVE); BasicDBObject queryObject = new BasicDBObject().append(QueryOperators.AND, new BasicDBObject[]{new BasicDBObject("indusId",rankParam.getIndusId()), new BasicDBObject("areaCode",startPattern),new BasicDBObject("areaCode",endPattern),new BasicDBObject("areaCode", new BasicDBObject("$not",notPattern)) }); findIterable = collection.find(queryObject); } MongoCursor<Document> mongoCursor = findIterable.iterator(); while (mongoCursor.hasNext()) { Document document = mongoCursor.next(); dataList.add(new Data(document.getString("name"), document.getDouble("value"), document.getString("date"), document.getString("areaCode"), document.getInteger("indusId"))); } }else{ } return dataList; }
java中操作Mongodb
标签:pre on() rank compile double ase iter pat 支持