mongoTemplate.aggregate()聚合查询,关联查询
聚合⽅法实现了什么功能
条件查询,分组,统计,关联查询,排序,分页,返回指定字段
聚合⽅法:
AggregationResults<DocumentEntity> results = mongoTemplate.aggregate(aggregation, CompanyId()), DocumentEntity.class);
源码:
@Override
public <O> AggregationResults<O> aggregate(Aggregation aggregation, String collectionName, Class<O> outputType) {
return aggregate(aggregation, collectionName, outputType, null);
}
参数1:Aggregation,看下这个对象的⽅法,可以看到⼊参有两种,集合/数组
public static Aggregation newAggregation(List<? extends AggregationOperation> operations) {
return Array(new AggregationOperation[operations.size()]));
}
public static Aggregation operations) {
return new Aggregation(operations);
}
1,关联查询
需求:有三张表,档案表,发票表,⽂件表,发票表和⽂件表通过字段⽂档流⽔号(documentSerialNum),与档案表字段(rialNum)流⽔号关联,现在要根据rialNum查询⽂档表及其下⾯的发票和发票信息(1:n)。
代码:
private List<DocumentEntity> getDocumentEntities(BaRequestModel request, List<String> rialNumS) {
// ⽂件和发票表通过外键查询
LookupOperation lookupOperation = wLookup().
from("table_file").
localField("rialNum").
foreignField("documentSerialNum").
as("docs");
LookupOperation lookupOperationinv = wLookup().
from("table_inv").
localField("rialNum").
foreignField("documentSerialNum").
as("docs2");
// 拼装具体查询信息
Criteria docCri = Criteria.where("docs").not().size(0);
docCri.and("rialNum").in(rialNumS);
docCri.and("isDel").is(Value());
docCri.and("docs.isDel").is(Value());
docCri.and("docs2.isDel").is(Value());
AggregationOperation match = Aggregation.match(docCri);
// 把条件封装成List
List<AggregationOperation> operations = new ArrayList<>();
operations.add(lookupOperation);
operations.add(lookupOperationinv);
operations.add(match);
// 构建 Aggregation
Aggregation aggregation = wAggregation(operations);
// 执⾏查询
AggregationResults<DocumentEntity> results = mongoTemplate.aggregate(aggregation, "table_doc", DocumentEntity.class);
// 或者⼊参为数组
Aggregation aggregation = wAggregation(lookupOperation,lookupOperationinv,Aggregation.match(docCri));
MappedResults();
}
特别注意的⼀点:主字段和外键对应哪个表,指的是mongoTemplate.aggregate(),⽅法中的表
localField:table_doc主字段 rialNum
foreignField:table_inv table_file 外键 documentSerialNum
LookupOperation lookupOperation = wLookup(). from("table_file"). localField("rialNum"). foreignField("documentSerialNum"). as("docs");
2,返回指定字段,分组,统计,排序,分页
gpm详细见原⽂:
需求:在订单表中,根据buyerNick分组,统计每个buyerNick的电话、地址、⽀付总⾦额以及总商品数,返回结果是CustomerDetail。bnu
/*
* project:列出所有本次查询的字段,包括查询条件的字段和需要搜索的字段;
* match:搜索条件criteria
* unwind:某⼀个字段是集合,将该字段分解成数组
* group:分组的字段,以及聚合相关查询
* sum:求和(同sql查询)
* count:数量(同sql查询)
* as:别名(同sql查询)
* addToSet:将符合的字段值添加到⼀个集合或数组中
tenniscourt* sort:排序
* skip&limit:分页查询
*/
public List<CustomerDetail> customerDetailList(Integer pageNum,String urId,String buyerNick,String itemId,List<String> phones) throws Exception{ Criteria criteria = Criteria.where("urId").is(urId);
Integer pageSize = 10;
Integer startRows = (pageNum - 1) * pageSize;
if(buyerNick != null && !"".equals(buyerNick)){
criteria.and("buyerNick").is(buyerNick);
}
if(phones != null && phones.size() > 0){
criteria.and("mobile").in(phoneList);
}
if(itemId != null && !"".equals(itemId)){
criteria.and("orders.numIid").is(itemId);
}
Aggregation customerAgg = wAggregation(
Aggregation.project("buyerNick","payment","num","tid","urId","address","mobile","orders"),
Aggregation.match(criteria),
Aggregation.unwind("orders"),
first("address").as("address").sum("payment").as("totalPayment").sum("num").as("itemNum").count().as("orderNum"),
Aggregation.sort(new Sort(new Sort.Order(Sort.Direction.DESC, "totalPayment"))),
Aggregation.skip(startRows),
Aggregation.limit(pageSize)
);
List<CustomerDetail> customerList = tradeRepository.findAggregateList(new Query(criteria), urId, customerAgg,CustomerDetail.class);
return customerList;
元旦快乐英语怎么说
}
public <T> List<T> findAggregateList(Query query,String urNickName, Aggregation aggregation,Class<T> clazz) {
AggregationResults<T> aggregate = Template.aggregate(aggregation, collectionName, clazz);
List<T> customerDetails = MappedResults();
return customerDetails;
}
3,三张表在java中实体如何关联的:字表定义为List
@Data
@ToString
public class DocumentEntity extends MongoBaEntity {
/**
* 档案名称
*/
private String name;
/**
* 档案类型
*/
private int type;
/**
london
* 所属公司
翻译在线韩语
*/
private Integer companyId;
/**
* 流⽔号
*/
private String rialNum;
/**
* 档案编号
*/
private String code;
/**
* 状态
*/
private String status;
/**
英语作文经典句子
* 保密等级
*/
private String crecyLevel;
/**
* 资料数量
*/
private Long detailNum;
/
**
* 描述
*/
private String description;
noneofyourbusiness
/**
* ⾃定义字段 map集合,
*/
private Map<String,Object> documentMap;
/**
* ⽂件集合
*/
private List<FileEntity> fileList;
/**
* 发票集合
*/
minori
private List<InvoiceEntity> InvoiceList;
}
>妇女节快乐英文