2 baidu 34019275 baidu_34019275 于 2016.02.19 14:33 提问

MongoTemplate().aggregate()做聚合查询返回结果不对

上代码:
Criteria c = Criteria.where("storeId")
.is(hotelId)
.and("bussDate")
.gte(startDate)
.lt(endDate);
if(t == 1){
c.and("dishAttr").nin(372);
}else{
c.and("dishAttr").nin(373);
}
//Query q = new Query(c);
if(pageNum == 1){
pageNum -= 1;
}else{
pageSize = pageSize * pageNum;
pageNum = (pageNum - 1) * 20;
}
//q.skip(pageNum).limit(pageSize);

    Aggregation aggregation = Aggregation.newAggregation(
            Aggregation.match(c),
            Aggregation.group("dishName").sum("makeNum").as("makeNum").sum("makeAmount").as("makeAmount"),
            Aggregation.skip(pageNum),
            Aggregation.limit(pageSize)
            );

    System.out.println("--mongoDBSQL--"+aggregation.toString());
    AggregationResults<RptDailyDishSale> aggRes = this.getMongoTemplate().aggregate(aggregation, "rptDailyDishSale",  RptDailyDishSale.class);
    List<RptDailyDishSale> listRes = aggRes.getMappedResults();
    for (RptDailyDishSale rptDailyDishSale : listRes)
    {
        System.out.println("--菜类名称:--"+rptDailyDishSale.getSortName()+"--菜品名称:--"+rptDailyDishSale.getDishNam());

        //**问题来了!!!!!**
        打印的rptDailyDishSale.getDishNam() 名称是 null
        debug的时候 截图如下
        ![图片说明](http://img.ask.csdn.net/upload/201602/19/1455863480_211441.jpg)

    }
    return listRes;

    打印出来的sql语句
    ----------------------------------
    --mongoDBSQL--{ "aggregate" : "__collection__" , "pipeline" : [ { "$match" : { "storeId" : "14e714b8-8c68-445c-a74a-3cc5a13afef2" , "bussDate" : { "$gte" : "2016-02-18" , "$lt" : "2016-02-19"} , "dishAttr" : { "$nin" : [ 373]}}} , { "$group" : { "_id" : "$dishName" , "makeNum" : { "$sum" : "$makeNum"} , "makeAmount" : { "$sum" : "$makeAmount"}}} , { "$skip" : 0} , { "$limit" : 20}]}

1个回答

baidu_34019275
baidu_34019275   2016.02.19 16:58

刚才的这个问题解决了 看代码:
需要给分组的加一个别名
Aggregation.project("dishName",).and("dishName").previousOperation()

Csdn user default icon
上传中...
上传图片
插入图片