package com.quanjude;
import com.quanjude.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.util.List;
import java.util.regex.Pattern;
@SpringBootTest
public class ApplicationTest {
@Autowired
private MongoTemplate mongoTemplate;
@Test
public void test01() {
// mongo的查询
// 简单查询
Criteria criteria = Criteria.where("字段").is("值");
// 字段不存在:不管字段是不是null,是不是空字符串,只要有这个字段,就查不出来
Criteria criteria1 = Criteria.where("字段").exists(false);
// 字段存在,不管字段是不是null,是不是空字符串,都可以查出来
Criteria criteria2 = Criteria.where("字段").exists(true);
// 字段为null:字段不存在或者存在并且=null都可以查出来,存在=“”和存在有值查不出来
Criteria criteria3 = Criteria.where("字段").isNull();
// 字段不为null:存在字段,且!=null,可以是“”或者有值
Criteria criteria4 = Criteria.where("字段").ne(null);
// 字段是空字符串:字段存在,且=“”
Criteria criteria5 = Criteria.where("字段").is("");
// 字段不存在或者存在!=“”的都可以查出来
Criteria criteria6 = Criteria.where("字段").ne("");
// 字段不是空字符串,不是null,有值
Criteria criteria7 = new Criteria().andOperator(
Criteria.where("字段").ne(null),
Criteria.where("字段").ne("")
);
// 条件1 and 条件2 and ...
Criteria criteria8 = new Criteria().andOperator(
Criteria.where("字段1").is("值1"),
Criteria.where("字段2").is("值2")
);
// 条件1 or 条件2 or ...
Criteria criteria9 = new Criteria().orOperator(
Criteria.where("字段1").is("值1"),
Criteria.where("字段2").is("值1")
);
// 条件1 and (条件2 or 条件 3)
Criteria criteria10 = new Criteria().andOperator(
Criteria.where("字段1").is("值1"),
new Criteria().orOperator(
Criteria.where("字段2").is("值2"),
Criteria.where("字段3").is("值3")
)
);
// 条件1 or (条件2 and 条件3 )
Criteria criteria11 = new Criteria().orOperator(
Criteria.where("字段1").is("值1"),
new Criteria().andOperator(
Criteria.where("字段2").is("值2"),
Criteria.where("字段3").is("值3")
)
);
// 模糊查询
String keyWord = "关键字";
Pattern pattern = Pattern.compile("^.*" + keyWord + ".*$", Pattern.CASE_INSENSITIVE); // 模糊匹配
Pattern leftPattern = Pattern.compile("^" + keyWord + ".*$", Pattern.CASE_INSENSITIVE); // 左侧模糊匹配
Pattern rightPattern = Pattern.compile("^.*" + keyWord + "$", Pattern.CASE_INSENSITIVE); // 右侧模糊匹配
Criteria criteria14 = Criteria.where("字段").regex(pattern);
Query query = new Query(criteria);
// 排序
Sort sort = Sort.by(Sort.Direction.DESC, "排序字段");
query.with(sort);
// 分页 (当前页-1)*每页大小,每页大小
int pageNum = 1;
int pageSize = 10;
query.skip((pageNum - 1) * pageSize).limit(pageSize);
List<User> userList = mongoTemplate.find(query, User.class);
System.out.println(userList);
// 聚合
Criteria criteria12 = Criteria.where("count").gt(0);
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(criteria), // 分组前的条件
// 要显示的字段,count不是数据库的字段,是下面取的别名,下面按哪个字段分组,这里一定要写哪个字段,不然报错,
// 而且分组这个字段,查出的结果里映射的是“_id”这个字段
Aggregation.project("性别", "count"), // 这里的count不写也可以,下面已经有了as字段,count就会显示出来的
Aggregation.group("性别").count().as("count"), // group是要分组的字段,count是统计个数,as是把统计的个数字段取别名为count
Aggregation.match(criteria12) // 分组后的条件,这里就是要取分组后统计个数大于0的数据,如果不过滤个数大于0的数据,就不要这行代码
);
// 按性别分组,对每组的分数score求和,过滤分数和大于100的数据
Criteria criteria13 = Criteria.where("sum").gt(100);
Aggregation aggregation1 = Aggregation.newAggregation(
Aggregation.match(criteria),
Aggregation.project("性别", "sum"),
Aggregation.group("性别").sum("score").as("sum"),
Aggregation.match(criteria13)
);
AggregationResults<User> aggregationResults = mongoTemplate.aggregate(aggregation, "user", User.class);
List<User> results = aggregationResults.getMappedResults();
System.out.println(results);
}
}
mongoTemplate条件查询,排序,分页,聚合分组查询
于 2024-05-24 11:15:53 首次发布