mongo 常用查询代码

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 返回数值的总和。忽略非数字值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值