当前位置:   article > 正文

java实现mongo查询使用总结_java mongodb 查询

java mongodb 查询

1.简单查询

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

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();
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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();
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/56090
推荐阅读
相关标签
  

闽ICP备14008679号