Spring的MongoTemplate提供了聚合查询的接口,我写了一个简单聚合查询
List<AggregationOperation> operations = new ArrayList();
operations.add(Aggregation.group(new String[]{"field1,filed2"}).count().as("count"));
operations.add(Aggregation.sort(Sort.Direction.DESC, "field1"));
operations.add(Aggregation.skip(skip));
operations.add(Aggregation.limit(limit));
operations.add(Aggregation.match(Criteria.where("field1").in(fields)));
Aggregation aggregation = Aggregation.newAggregation(operations);BicDBObject.class);
AggregationResults<BasicDBObject> results = mongoDdlpdbTemplate.aggregate(aggregation, Bean.class, BasicDBObject.class);
结果查出来结果是空.
刚开始考虑是不是查询条件出问题了,于是原样查询改成Query
Query query = new Query();
query.with(new Sort(Sort.Direction.DESC, "field1"));
query.skip(skip);
query.limit(limit);
query.addCriteria(Criteria.where("field1").in(fields));
List<Bean> list = mongoDdlpdbTemplate.find(query,Bean.class);
查询结果没问题
于是猜测是不是group影响了查询
去掉group查询
List<AggregationOperation> operations = new ArrayList();
operations.add(Aggregation.sort(Sort.Direction.DESC, "field1"));
operations.add(Aggregation.skip(skip));
operations.add(Aggregation.limit(limit));
operations.add(Aggregation.match(Criteria.where("field1").in(fields)));
Aggregation aggregation = Aggregation.newAggregation(operations);
AggregationResults<BasicDBObject> results = mongoDdlpdbTemplate.aggregate(aggregation, Bean.class, BasicDBObject.class);
结果没问题,这样明显是查询语句放的地方不对了,于是调整查询语句顺序,放在group前
List<AggregationOperation> operations = new ArrayList();
operations.add(Aggregation.match(Criteria.where("field1").in(fields)));
operations.add(Aggregation.group(new String[]{"field1,filed2"}).count().as("count"));
operations.add(Aggregation.sort(Sort.Direction.DESC, "field1"));
operations.add(Aggregation.skip(skip));
operations.add(Aggregation.limit(limit));
Aggregation aggregation = Aggregation.newAggregation(operations);BicDBObject.class);
AggregationResults<BasicDBObject> results = mongoDdlpdbTemplate.aggregate(aggregation, Bean.class, BasicDBObject.class);
查询结果正常
但是我去查了下资料,mongo语法里面是没有说查询语句要在聚合语句之前的,具体原因有待探究
Spring的MongoTemplate提供了聚合查询的接口,我写了一个简单聚合查询List&lt;AggregationOperation&gt; operations = new ArrayList();operations.add(Aggregation.group(new String[]{"field1,filed2"}).count().as("count"));operation...
pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven...
* project:列出所有本次查询的字段,包括查询条件的字段和需要搜索的字段;
* match:搜索条件criteria
* unwind:某一个字段是集合,将该字段分解成数组
* group:分组的字段,以及聚合相关查询
* sum:求和(同sql查询)
//Spring Data MongoDB隆重登场
public Map groupBySpringDataMongoDB(){
List questionIdList = distinctByIsRight();
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteri
模糊查询:regex;
分页查询,每页多少:skip,limit
按某个字段排序(或升或降):new Sort(new Sort.Order(Sort.Direction.ASC, "port"))
查询数量:count
Criteria criteria = new Criteria();//条件查询
criteria.andOperator(Criteria....
一,mongodb聚合介绍
db.collection.aggregate()是基于数据处理的聚合管道,每个文档通过一个由多个阶段(stage)组成的管道,可以对每个阶段的管道进行分组、过滤等功能,然后经过一系列的处理,输出相应的结果。
通过这张图,可以了解Aggregate处理的过程。
1、db.collection.aggregate() 可以用多个构件创建一个管道,对...
第1个参数是“给哪一列创建索引”,它的格式是: {key:1或-1},其中1表示升序,-1表示降序。
第2个参数是 “额外选项”,它是可选的,比如:设置索引名称、指定索引类型等等。
举例:db.集合名.createIndex({age:1}) ; ...
Due to limitations of the com.mongodb.BasicDocument, you can't add a second '$and' expression specified as '$and :
错误原因:在一个 Criteria 对象中调用了多次 andOperator() 方法
使用mongoTemplate实现...
理解numpy的rollaxis与swapaxes函数
机器不会学习: