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    展开数组(数组内容拆分显示)    无

$facet:用于在单个聚合管道中执行多个聚合操作,并将结果分组输出。

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 不等于


       criteriaInfo.and("Time").nin(值);

6 ne 不等于某个值的条件
Criteria.where("Status").ne(0)

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

6 then(1).otherwise(0)

 是一个伪代码,用来表示在数据聚合操作中的条件逻辑。它的作用是根据条件的满足与否分别赋值不同的结果

new Criteria().andOperator(
        Criteria.where("Code").in(值)
)).then(1).otherwise(0))

then 方法用来指定条件满足时的赋值结果,otherwise 方法用来指定条件不满足时的赋值结果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值