MongoDb Aggregation.project()与Aggregation.group().sum()在Springboot中的用法问题

sonicxia 2017-12-22 02:41:24
涉及到的代码如下:

List<AggregationOperation> operations = new ArrayList<AggregationOperation>();
operations.add(
Aggregation.project().andExpression("substr(startTime,0,10)").as("day")
);
operations.add(
Aggregation.group("day")
.sum("$duration").as("totalDuration")
);
operations.add(
Aggregation.match(
Criteria.where("ccId").is(ccId)
.and("startTime").gte(dateFrom).lte(dateTo)
)
);
operations.add(
Aggregation.sort(Sort.Direction.ASC, "_id") // 按日期升序排序
);
Aggregation aggregation = Aggregation.newAggregation(operations);
AggregationResults<BasicDBObject> results = mongoTemplate.aggregate(
aggregation, "t_call_record", BasicDBObject.class);
List<BasicDBObject> mappedResults = results.getMappedResults();

断点调试,执行到

AggregationResults<BasicDBObject> results = mongoTemplate.aggregate(
aggregation, "t_call_record", BasicDBObject.class);

代码时就报错了,提示:

java.lang.IllegalArgumentException: Invalid reference '$duration'!

“duration”在mongodb中是集合的一个key,业务需求是按日统计duration key对应value的累加值。
我在另一个业务方法里将Aggregation.project() 和 Aggregation.group("day") .count().as("totalCnt")一起用来统计次数都没有报错,心想同样是聚合行数,sum()和count()的用法应该是差不多的。在我多次尝试后,发现如果删除

operations.add(
Aggregation.group("day")
.sum("$duration").as("totalDuration")
);

这段代码,错误就不会报了,但也不符合我的业务逻辑。
所以就想请教各位大神,在springboot中使用mongoTemplate相关语法进行mongodb的交互操作时,若想Aggregation.project() 与 Aggregation.group().sum()联合使用,是否还有其他条件限制或语法规范?谢谢!
...全文
5581 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wednesday89 2018-09-25
  • 打赏
  • 举报
回复
operations.add(
Aggregation.project("duration").andExpression("substr(startTime,0,10)").as("day")
);
operations.add(
Aggregation.group("day")
.sum("duration").as("totalDuration")
);
方丈的寺院 2018-02-24
  • 打赏
  • 举报
回复
$duration 表示的是一个变量啊 直接用duration
sonicxia 2017-12-25
  • 打赏
  • 举报
回复
自己顶下,不要这么快沉啊
Mingo 是 MongoDB 查询语言的 JavaScript 实现。Mingo 利用 MongoDB 风格查询,在客户端或者服务器端环境下,允许直接查询内存的 JavaScript 对象。特性:Comparisons Operators ($gt, $gte, $lt, $lte, $ne, $nin, $in)Logical Operators ($and, $or, $nor, $not)Evaluation Operators ($regex, $mod, $where)Array Operators ($all, $elemMatch, $size)Element Operators ($exists, $type)Aggregation Pipeline Operators ($group, $match, $project, $sort, $limit, $unwind, $skip)Conditional Operators ($cond, $ifNull)Group Operators ($addToSet, $sum, $max, $min, $avg, $push, $first, $last)Arithmetic Operators ($add, $divide, $mod, $multiply, $subtract)String Operators ($cmp, $strcasecmp, $concat, $substr, $toLower, $toUpper)Set Operators ($setEquals, $setIntersection, $setDifference, $setUnion, $setIsSubset, $anyElementTrue, $allElementsTrue)Projection Operators ($elemMatch, $slice)JSON stream filtering and projection. NodeJS only使用var Mingo = require('mingo'); // or just access *Mingo* global in browser // setup the key field for your collection Mingo.setup({     key: '_id' // default }); // create a query with criteria // find all grades for homework with score >= 50 var query = new Mingo.Query({     type: "homework",     score: { $gte: 50 } });搜索和过滤// filter collection with find() var cursor = query.find(collection); // shorthand with query criteria // cursor = Mingo.find(collection, criteria); // sort, skip and limit by chaining cursor.sort({student_id: 1, score: -1})     .skip(100)     .limit(100); // count matches cursor.count(); // iterate cursor // iteration is forward only while (cursor.hasNext()) {     console.log(cursor.next()); } // use first(), last() and all() to retrieve matched objects cursor.first(); cursor.last(); cursor.all(); // Filter non-matched objects ( var result = query.remove(collection); 标签:Mingo

1,746

社区成员

发帖
与我相关
我的任务
社区描述
MongoDB相关内容讨论区
社区管理员
  • MongoDB社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧