java mongodb多条件聚合查询

业务逻辑,

查询符合 :1、大于且小于时间 2、按atype2字段筛选  3、按人数字段求和  4、按省字段聚合

下面代码,可以直接拿来用。

public List deathnumberByProvince(String startDate, String endDate,String type2) {

        List optionList = new ArrayList<>();  //mongodb查询
        BasicDBObject gteMoneyStart = new BasicDBObject("originaltime",new BasicDBObject("$gte", startDate) );
        BasicDBObject matchMoneyStart = new BasicDBObject("$match", gteMoneyStart);
        optionList.add(matchMoneyStart);
        BasicDBObject gteMoneyEnd = new BasicDBObject("originaltime",new BasicDBObject("$lte", endDate) );
        BasicDBObject matchMoneyEnd = new BasicDBObject("$match", gteMoneyEnd);
        optionList.add(matchMoneyEnd);

        BasicDBObject query = new BasicDBObject("atype2", type2);
        BasicDBObject matchProvince = new BasicDBObject("$match", query);
        optionList.add(matchProvince);

        DBObject groupFields=new BasicDBObject();
        groupFields = new BasicDBObject( "_id", "$province");
        groupFields.put("deathnumber", new BasicDBObject( "$sum", "$deathnumber"));
        DBObject group = new BasicDBObject("$group", groupFields);
        optionList.add(group);

        DBCollection collection = MongoDBUtil.getDBCollection("acc");
        List results = (List) collection.aggregate(optionList).results();
        return results;
    }

 

你可能感兴趣的:(MongoDB)