Mongo整合SpringTemplate用法

Mongo整合SpringTemplate用法

使用Spring的MongoTemplate如何执行自定义的mongo语句

对GoodsInfo Collection按照name进行distinct查询

String jsonSql = "{distinct:'GoodsInfo', key:'name'}";
CommandResult  commandResult = mongoTemplate.executeCommand(jsonSql);
System.out.println();
BasicDBList list = (BasicDBList)commandResult.get("values"); 
for (int i = 0; i < list.size(); i ++) { 
    System.out.println(list.get(i)); 
} 
System.out.println();

查询统计某一个时间段每个片区下的总用户数,新用户数,旧用户数,异常数

public Map<String, LandPushTypeCount> findTodayCountByAreaIds(Collection<String> areaIds) throws Exception {
        Map<String, LandPushTypeCount> result = new HashMap<String, LandPushTypeCount>();

        Criteria criteria = Criteria.where("todayDate").is(DateUtil.parseDate2YMD(new Date())).andOperator(Criteria.where("areaId").in(areaIds));
        String initDocument = "{codeCount:0, newCount:0, oldCount:0, errorCount:0}";
        String reduceFunction = "function (doc, prev) { prev.codeCount += doc.codeCount; prev.newCount += doc.newCount; prev.oldCount += doc.oldCount; prev.errorCount += doc.errorCount; }";
        GroupBy groupBy = GroupBy.key("areaId").initialDocument(initDocument).reduceFunction(reduceFunction);

        GroupByResults<LandPushTypeCount> groupByResult = landPushTypeCountDao.getMongoTemplate().group(criteria, "LandPushTypeCount", groupBy, LandPushTypeCount.class);

        BasicDBList list = (BasicDBList) groupByResult.getRawResults().get("retval");
        for (int i = 0; i < list.size(); i ++) {
            LandPushTypeCount landPushTypeCountBean = new LandPushTypeCount();
            BasicDBObject obj = (BasicDBObject)list.get(i);
            System.out.println("片区:" + obj.get("areaId")
                            + "总数量:" + obj.get("codeCount")
                            + "新用户数量:" + obj.get("newCount")
                            + "旧用户数量:" + obj.get("oldCount")
                            + "异常数量:" + obj.get("errorCount")
            );
            landPushTypeCountBean.setCodeCount(((Double) obj.get("codeCount")).intValue());
            landPushTypeCountBean.setNewCount(((Double) obj.get("newCount")).intValue());
            landPushTypeCountBean.setOldCount(((Double) obj.get("oldCount")).intValue());
            landPushTypeCountBean.setErrorCount(((Double) obj.get("errorCount")).intValue());
            result.put((String) obj.get("areaId"), landPushTypeCountBean);
        }
        return result;
    }

// 相当于mongo语句如下
db.runCommand(
{
  "group":
  {
    "ns":"LandPushTypeCount",
    "key":{"areaId":true},
    "initial":{codeCount:0, newCount:0, oldCount:0, errorCount:0},
    "$reduce":function(doc,prev)
    {
      prev.codeCount += doc.codeCount;
          prev.newCount += doc.newCount;
          prev.oldCount += doc.oldCount;
          prev.errorCount += doc.errorCount;
    },
    "condition":{"todayDate":"2015-10-29", "areaId":{$in:["", "330100", "110000"]}}
  }
}
);

查询top100回复数的帖子

public void top100Postinfo() {
        long start = System.nanoTime();
        try {
            final long startTime = 1443628800000L;
            final long endTime = 1446436799000L;

            Query query = new Query();
            query.addCriteria(Criteria.where("createTS").gte(startTime).lte(endTime));
            query.addCriteria(Criteria.where("srcPostId").exists(true));
            query.addCriteria(Criteria.where("referInfo").exists(false));
            query.addCriteria(Criteria.where("dr").is(0));
            long count = postinfoDAO.getCount(query);
            System.out.println("count:" + count);

            final int filterCount = 100;

            DBObject result1 = postinfoDAO.getMongoTemplate().execute(Postinfo.class, new CollectionCallback<DBObject>() {
                @Override
                public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException {

                    // match匹配条件
                    Map matchMap = new HashMap();
                    matchMap.put("createTS", new BasicDBObject("$gt", startTime).append("$lt", endTime));
                    matchMap.put("referInfo", new BasicDBObject("$exists", false));
                    matchMap.put("srcPostId", new BasicDBObject("$exists", true));
                    matchMap.put("dr", 0);
                    DBObject matchOption = new BasicDBObject("$match", matchMap);

                    // group条件
                    Map groupMap = new HashMap();
                    groupMap.put("_id", "$srcPostId");
                    groupMap.put("total", new BasicDBObject("$sum", 1));
                    BasicDBObject groupOption = new BasicDBObject("$group", groupMap);

                    // sort条件
                    Map sortMap = new HashMap();
                    sortMap.put("total", -1);
                    BasicDBObject sortOption = new BasicDBObject("$sort", sortMap);

                    // limit条件
                    BasicDBObject limitOption = new BasicDBObject("$limit", filterCount);

                    // 获取结果集
                    AggregationOutput output = collection.aggregate(matchOption, groupOption, sortOption, limitOption);
                    Iterator<DBObject> iterator = output.results().iterator();
                    DBObject result = null;
                    List<Map<String, String>> resultList = new ArrayList<Map<String, String>>();
                    while (iterator.hasNext()) {
                        result = iterator.next();
                        String id = (String) result.get("_id");
                        Integer total = (Integer) result.get("total");
                        System.out.println("id:" + id);
                        System.out.println("total:" + total);

                        Map<String, String> resultMap = new HashMap<>();
                        resultMap.put("id", id);
                        resultMap.put("total", String.valueOf(total));
                        resultList.add(resultMap);
                    }
                    exportExcel(resultList);
                    return result;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        long finish = System.nanoTime();
        System.out.println(TimeUnit.NANOSECONDS.toSeconds(finish - start) + ":秒");
        System.out.println("top20PercentPostinfo 执行完毕");
    }


// 相当于mongo语句如下
db.Postinfo.aggregate([
    { $match: { "createTS":{"$gt":1443628800000, "$lt":1446436799000}, "srcPostId":{$exists:true}, "referInfo":{$exists:false}, "dr":0 } },
    { $limit: 1 },
    { $group: { _id: "$srcPostId", total: { $sum: 1 } } },
    { $sort: { total: -1 } }
]);

参考文章:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值