MongoTemplate关联、分页、聚合

关联查询

一对多的结构,最终结果在一个json列表里,每一个集合1里有一个集合2的列表。

    /*
    * 多对一查询
    * 集合多的名字,集合多对应字段,集合一对应字段
    */
    public List selectTwoCol(String col1,String word,String word2){
        LookupOperation lookupOperation=LookupOperation.newLookup().from(col1).localField(word).foreignField(word2).as("col");
        //总数查询
        Aggregation aggregation = Aggregation.newAggregation(lookupOperation);
        List results = mongoTemplate.aggregate(aggregation, "AddMoney", Document.class).getMappedResults();
        //解析过程
        List list=new ArrayList<>();
        for (Document b:results){
            JSONObject jsonObject = JSONUtil.parseObj(b.toJson());
            list.add(jsonObject);
        }
        return list;
    }

 

分页查询

分页工具

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import java.io.Serializable;

/**
 * @author 张烁
 */
public class MongoPageable implements Serializable, Pageable {
    private static final long serialVersionUID = 1L;

    // 当前页
    private Integer pagenumber = 1;
    // 当前页面条数
    private Integer pagesize = 10;
    // 排序条件
    private Sort sort;

    public MongoPageable(Integer pagenumber, Integer pagesize, Sort sort) {
        this.pagenumber = pagenumber;
        this.pagesize = pagesize;
        this.sort = sort;
    }

    @Override
    public Pageable first() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getOffset() {
        // TODO Auto-generated method stub
        return (pagenumber - 1) * pagesize;
    }

    @Override
    public int getPageNumber() {
        // TODO Auto-generated method stub
        return pagenumber;
    }

    @Override
    public int getPageSize() {
        // TODO Auto-generated method stub
        return pagesize;
    }


    @Override
    public boolean hasPrevious() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Pageable next() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Pageable previousOrFirst() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Sort getSort() {
        // TODO Auto-generated method stub
        return sort;
    }
}

查询

排序与分页,with那一步拼接了mongo的skip和limit。

public Page paginationQuery(Integer pageNum) {
        Query query = new Query();
        Sort sort = new Sort(Sort.Direction.DESC, "money");
        //当前页码,每页条数,排序方式
        MongoPageable pageable = new MongoPageable(pageNum,5,sort);

        // 查询出一共的条数
        Long count = mongoTemplate.count(query, AddMoney.class);
        // 查询
        List list = mongoTemplate.find(query.with(pageable), AddMoney.class);
        // 将集合与分页结果封装
        Page pagelist = new PageImpl<>(list, pageable, count);
        return pagelist;
    }

使用聚合函数

统计点击数与计费的小例子

        Criteria criteria = Criteria.where("id").is(id).and("date").is(date);
        TypedAggregation agg = Aggregation.newAggregation(CostTidy.class,
                Aggregation.match(criteria),
                Aggregation.group("hour")
                        .sum("point").as("convertCntAll")
                        .addToSet("browsedMoney").as("browsedMoneyTotal")
                        .addToSet("convertMoney").as("convertMoneyTotal")
        );

        AggregationResults result = mongoTemplate.aggregate(agg, "cost", Document.class);

第一步设置查询条件

第二步分组计数,addToSet分组查询

返回结果集没有对应到某一个类,所以类型是Document,AggregationResults是mongo的一个json表,类似list,就是分组的结果集。

你可能感兴趣的:(NoSQL)