MongoTemplate 常用查询

MongoTemplate是Spring Data MongoDB提供的一个Java编程接口,用于操作MongoDB数据库。它提供了一系列方法和功能,以便进行数据的插入、更新、删除和查询等操作。

使用MongoTemplate,你可以通过编写Java代码与MongoDB进行交互,而无需直接编写原生的MongoDB查询语句。它提供了一些便捷的方法,如save、insert、update和remove,用于对文档进行增删改操作。同时,它还支持复杂的查询条件和排序,以及聚合管道查询等高级功能。

1 引用pom

<!--mongodb-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

2 Aggregation

     是一种强大的数据处理操作,它允许你对集合中的文档进行多个阶段的处理和转换,以生成复杂的结果。

使用聚合操作,你可以对文档进行分组、排序、过滤、投影、计算字段值、连接多个集合等操作,以实现更复杂的数据处理需求

$match    过滤数据,输出符合条件文档    where
$project    修改输入文档结构(筛选展示文档的键)    个人理解(select)
$limit    限制计算文档结果返回数    limit
$sort    文档排序    order by
$group    文档分组    group by
$skip    跳过指定数量的文档    skip
$unwind    展开数组(数组内容拆分显示)    无

3  查询方法

    @Override
    public List<Test> getList(Test search) {


       1 区间查询


        Criteria   criteriaInfo =
                Criteria.where("Time").gte(search.getStartTime()).lte(search.getEndTime());

       2 in 查询


         criteriaInfo.and("Code").in(List<String>);


       3  or 查询 两个满足1


         criteriaInfo.orOperator(
                    Criteria.where("title").regex(值),
                    Criteria.where("name").regex(值)
            );

     4 is 等于


       criteriaInfo.and("Status").is(值);

    5 nin 不等于


        Criteria.where("Time").nin(值);
 
        List<AggregationOperation> aggregationList = new ArrayList<>();
        // Match操作,筛选 
        aggregationList.add(Aggregation.match(  criteriaInfo));

        // 只查出 Test 中的字段 相当于 select *(字段)
        aggregationList.add(
                Aggregation.project("Time")
                       
        );
        Aggregation aggregation = Aggregation.newAggregation(aggregationList);
        List<Test> result;
        result = mongoTemplate.aggregate(
                aggregation, "Test", Test.class).getMappedResults();
        return null;
    }


 
2 查询左连

    @Override
    public List<Test> getList(Test search) {
        Criteria criteria =
                Criteria.where("Time").gte(search.getStartTime()).lte(search.getEndTime());
        List<AggregationOperation> aggregationList = new ArrayList<>();
        // Match操作,筛选 
        aggregationList.add(Aggregation.match(criteria));

        // 只查出 Test 中的字段
        aggregationList.add(
                Aggregation.project("Time") //显字段
                     .andExpression("title2").as("title")  //替换显示字段                   .andExpression("ifNull(status, 0)").as("status")  //替换显示字段   

        );
       // Lookup操作 左连接
        aggregationList.add(LookupOperation.newLookup()
                .from("test2")            //关联表名
                .localField("Time")     // 主表中的关联字段
                .foreignField("Time")  //从表关联的字段
                .as("test3"));    //查询结果表名

        Aggregation aggregation = Aggregation.newAggregation(aggregationList);
        List<Test> result;
        result = mongoTemplate.aggregate(
                aggregation, "Test", Test.class).getMappedResults();
        return null;
    }

3 查询总记数
Map total = mongoTemplate.aggregate(aggregation, "test", Map.class).getUniqueMappedResult();
4 分页

   List<AggregationOperation> aggregationList = new ArrayList<>();

aggregationList.add(Aggregation.skip((search.getPageNum() - 1) * search.getPageSize()));
aggregationList.add(Aggregation.limit(search.getPageSize()));

5 排序

List<Test> dataList= new ArrayList<>(); dataList.sort(Comparator.comparing(Test::getTime).thenComparing(Test::getcode));

相关推荐

  1. MongoTemplate 查询

    2023-12-08 16:34:02       27 阅读
  2. postgres查询

    2023-12-08 16:34:02       8 阅读
  3. es查询编辑

    2023-12-08 16:34:02       32 阅读
  4. ES查询方式

    2023-12-08 16:34:02       14 阅读
  5. 金蝶数据库查询

    2023-12-08 16:34:02       31 阅读
  6. ElasticSearch基础及查询

    2023-12-08 16:34:02       40 阅读
  7. 【ElasticSearch】查询格式介绍

    2023-12-08 16:34:02       31 阅读
  8. Linux查询日志命令整理

    2023-12-08 16:34:02       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-08 16:34:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-08 16:34:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-08 16:34:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-08 16:34:02       20 阅读

热门阅读

  1. C/C++ 快速排序

    2023-12-08 16:34:02       37 阅读
  2. C语言-动态内存分配

    2023-12-08 16:34:02       35 阅读
  3. 一文详解:什么是https 加密协议?

    2023-12-08 16:34:02       35 阅读
  4. GO设计模式——5、建造者模式(创建型)

    2023-12-08 16:34:02       27 阅读
  5. 写一个简单的达梦数据库巡检脚本

    2023-12-08 16:34:02       37 阅读