1.多条件排序
List<Sort.Order> list = new ArrayList<>();
list.add(new Sort.Order(Sort.Direction.DESC, "publishTime"));
list.add(new Sort.Order(Sort.Direction.ASC, "createTime"));
Sort sort = Sort.by( list );
2. 多条件查询
Query query = new Query();
Criteria criteria = Criteria.where( "status" ).is( 0 );
criteria.and( "totalScore" ).lte( exist.getTotalScore() );//lte
criteria.and( "noteId" ).ne( noteId );
criteria.and( "type" ).is( NoteTypeEnum.IMAGE.toString() );
query.addCriteria(criteria );
long count = mongoTemplate.count( query, NoteScore.class );
query.with( Sort.by( Sort.Direction.DESC, "totalScore" ) );
query.with( PageRequest.of( pageNum - 1, pageSize ) );
List<NoteScore> noteScores = mongoTemplate.find( query, NoteScore.class );
3.两字段段求和
ArithmeticOperators.Add.valueOf( “totalScore” ).add( “weightScore” )
应用
List<AggregationOperation> operations = new ArrayList<>();
Criteria criteria = Criteria.where( "status" ).in( getEffectiveStatus() );
operations.add(Aggregation.match(criteria));
//还可以根据_id 分组 totalScore+weightScore 排序
operations.add( Aggregation.group( "noteId" ).first("noteId").as( "noteId" ).sum( ArithmeticOperators.Add.valueOf( "totalScore" ).add( "weightScore" ) ).as( "totalScore" ));
long count = mongoTemplate.aggregate( Aggregation.newAggregation( operations ), "noteScore", NoteScore.class ).getMappedResults().size();
operations.add( Aggregation.sort( Sort.by( Sort.Direction.DESC, "totalScore" ) ) );
operations.add( Aggregation.skip( Long.valueOf( pageNum - 1 ) * pageSize ) );
operations.add( Aggregation.limit( pageSize ) );
AggregationResults<NoteScore> article = mongoTemplate.aggregate( Aggregation.newAggregation( operations ), "noteScore", NoteScore.class );
4.分组后再按条件过滤
List<AggregationOperation> operations = new ArrayList<>();
//分组前条件
Criteria criteria = Criteria.where( "status" ).in( getEffectiveStatus() );
criteria.and( "noteId" ).ne( noteId );
criteria.and( "type" ).is( NoteTypeEnum.VIDEO.toString() );
operations.add(Aggregation.match(criteria));
operations.add( Aggregation.group( "noteId" ).first("noteId").as( "noteId" ).sum( ArithmeticOperators.Add.valueOf( "totalScore" ).add( "weightScore" ) ).as( "totalScore" ));
//分组后条件
operations.add( Aggregation.match(Criteria.where( "totalScore" ).lte( exist.getTotalScore()+exist.getWeightScore() )) );
long count = mongoTemplate.aggregate( Aggregation.newAggregation( operations ), "noteScore", NoteScore.class ).getMappedResults().size();
operations.add( Aggregation.sort( Sort.by( Sort.Direction.DESC, "totalScore" ) ) );
operations.add( Aggregation.skip( Long.valueOf( pageNum - 1 ) * pageSize ) );
operations.add( Aggregation.limit( pageSize ) );
AggregationResults<NoteScore> article = mongoTemplate.aggregate( Aggregation.newAggregation( operations ), "noteScore", NoteScore.class );
List<NoteScore> noteScores = article.getMappedResults();
5.条件或与
Criteria criteria = Criteria.where( "status" ).is( 0 );
Criteria criteria2 = new Criteria();
criteria2.orOperator(Criteria.where( "totalScore" ).lte( exist.getTotalScore() ),Criteria.where("createTime").gt( exist.getCreateTime() ) );
Criteria criteriac = new Criteria();
criteriac.andOperator( criteria,criteria2 );
- project:列出所有本次查询的字段,包括查询条件的字段和需要搜索的字段;
- match:搜索条件criteria
- unwind:某一个字段是集合,将该字段分解成数组
- group:分组的字段,以及聚合相关查询
》 sum:求和(同sql查询)
》 count:数量(同sql查询)
》 as:别名(同sql查询)
》 addToSet:将符合的字段值添加到一个集合或数组中 - sort:排序
- skip&limit:分页查询
$group 分组聚合
累加运算符
该操作必须是以下蓄电池运营商之一:
名称 描述
$avg 返回数值的平均值。忽略非数字值。
$first 返回每个组的第一个文档中的值。仅在文档按定义的顺序定义时才定义订单。
$last 返回每个组的最后一个文档的值。仅在文档按定义的顺序定义时才定义订单。
$max 返回每个组的最高表达式值。
$min 返回每个组的最低表达式值。
$push 返回每个组的表达式值数组。
$addToSet 返回每个组的唯一表达式值数组。数组元素的顺序未定义。
$stdDevPop 返回输入值的总体标准差。
$stdDevSamp 返回输入值的样本标准偏差。
$sum 返回数值的总和。忽略非数字值。