java实现mongo查询使用总结

1.简单查询

    public <T>List<T> findQuery(Criteria criteria, Class<T> c ,String tableName) {
        Query query = Query.query(criteria);
        return mongoTemplate.find(query, c, tableName);
    }

2.聚合查询

  public List<Map> aggregateQuery(Criteria criteria, String groupField, String tableName) {
        AggregationOptions aggregationOptions = AggregationOptions.builder().allowDiskUse(true).build();
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group(groupField).count().as("count")
        ).withOptions(aggregationOptions);
        return mongoTemplate.aggregate(aggregation, tableName, Map.class).getMappedResults();
    }

3.复杂聚合查询


  public List<Map> complexAggregateQuery(Criteria criteria, String primaryField, String additionalField, String tableName) {
        AggregationOptions aggregationOptions = AggregationOptions.builder().allowDiskUse(true).build();
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group(primaryField, additionalField),
                Aggregation.group(primaryField).count().as("count")
        ).withOptions(aggregationOptions);
        return mongoTemplate.aggregate(aggregation, tableName, Map.class).getMappedResults();
    }

4.聚合排序limit条数

    public List<Map> aggregateSortQuery(Criteria criteria, String groupField, String tableName, long limit) {
        AggregationOptions aggregationOptions = AggregationOptions.builder().allowDiskUse(true).build();
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group(groupField).count().as("count"),
                Aggregation.sort(new Sort(new Sort.Order(Sort.Direction.DESC,"count"))),
                Aggregation.limit(limit)
        ).withOptions(aggregationOptions);
        return mongoTemplate.aggregate(aggregation, tableName, Map.class).getMappedResults();
    }

4.聚合排序返回对象

   public<T>List<T> aggregateGroupQueryT(Criteria criteria, String groupField, String tableName,Class<T> c) {
        AggregationOptions aggregationOptions = AggregationOptions.builder().allowDiskUse(true).build();
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group(groupField)
        ).withOptions(aggregationOptions);
        return mongoTemplate.aggregate(aggregation, tableName, c).getMappedResults();
    }

5.聚合排序加limit条数返回对象

    public<T>List<T> aggregateSortQueryT(Criteria criteria, String groupField, String tableName, long limit, Class<T> c) {
        AggregationOptions aggregationOptions = AggregationOptions.builder().allowDiskUse(true).build();
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.match(criteria),
                Aggregation.group(groupField).count().as("count"),
                Aggregation.sort(new Sort(new Sort.Order(Sort.Direction.DESC,"count"))),
                Aggregation.limit(limit)
        ).withOptions(aggregationOptions);
        return mongoTemplate.aggregate(aggregation, tableName, c).getMappedResults();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java使用MongoDB进行查询时,可以使用org.springframework.data.mongodb包中的Query类来构建查询语句。例如,可以使用Query的addCriteria方法来添加查询条件,如Criteria.where("aa").is("dd")表示查询条件为aa等于"dd"。多个条件可以无限添加。然后可以使用mongoTemplate.find方法执行查询,相当于SQL中的select语句,查询结果为满足条件的所有文档。例如,mongoTemplate.find(query, userEntity.class)表示查询userEntity集合中满足条件hh等于1且aa等于"dd"的所有文档。\[1\]\[2\] 如果想要让查询结果按照文档中的某个字段进行排序,可以在find方法之后调用sort方法。例如,可以使用Filters.eq("batch", 1)来指定按照batch字段进行排序,其中1代表升序,-1代表降序。可以使用Filters.and方法将多个过滤条件组合在一起。另外,也可以使用MongoTemplate对结果进行排序。\[3\] #### 引用[.reference_title] - *1* *2* [java mongo查询(包括集合中数组的查询等)(持续更新中)](https://blog.csdn.net/bokestudy/article/details/105435686)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [java操作mongo查询排序及指定返回结果包含的字段](https://blog.csdn.net/weixin_55109878/article/details/123628163)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值