简介
说明
本文用示例介绍Spring Data MongoDB(MongoTemplate)查询数据的方法。
查询方法分类
- mongoTemplate.find //返回list
- mongoTemplate.findOne //返回1个(查询1个)
- mongoTemplate.findAll // 返回list(查询所有)
简单查询
@Test
public void testEqual2() {
// 使用静态方法创建 Criteria
Criteria criteria = Criteria.where("press").is("清华大学出版社");
Query query = new Query(criteria);
final List<Book> books = mongoTemplate.find(query, Book.class, "book");
assert books.size() == 3;
}
支持的查询有:
- 等于:is
- 大于:gt
- 小于:lt
- 大于等于:gte
- 小于等于:lte
- 不等于:ne
- 模:mod
多条件查询
@Test
public void testAnd() {
Criteria criteria = new Criteria();
criteria.and("price")
.gt(45)
.and("press").is("清华大学出版社");
Query query = new Query(criteria);
List<Book> books = mongoTemplate.find(query, Book.class, BOOK_COLLECTION);
}
排序与分页
@Test
public void testSortAndLimit() {
Criteria criteria = Criteria.where("press").is("清华大学出版社");
Query query = new Query(criteria);
// with 方法设置排序
query.with(Sort.by(Sort.Direction.ASC, "price"));
// skip limit 方法设置分页
query.skip(1).limit(10);
final List<Book> books = mongoTemplate.find(query, Book.class, "book");
assert books.size() ==2;
}
正则查询
@Test
public void testContain(){
String name = "开发";
Pattern pattern = Pattern.compile("^.*" + name + ".*$", Pattern.CASE_INSENSITIVE);
Criteria criteria = Criteria.where("name").regex(pattern);
Query query = new Query(criteria);
final List<Book> books = mongoTemplate.find(query, Book.class, "book");
assert books.size() == 1;
}
数组查询
匹配数组中单个值
// 数组中包含查询单值
@Test
public void testArrayMatchSingle() {
Criteria criteria = Criteria.where("tags").is("编程");
Query query = new Query(criteria);
List<Book> books = mongoTemplate.find(query, Book.class, "book");
}
匹配数组中多个值
// 数组中同时包含指定的多个值,不要求顺序
@Test
public void testArrayMatchAll() {
Criteria criteria = Criteria.where("tags").all("编程", "程序设计");
Query query = new Query(criteria);
List<Book> books = mongoTemplate.find(query, Book.class, "book");
}
匹配数组中元素个数
@Test
public void testArrayMatchSize() {
Criteria criteria = Criteria.where("tags").size(4);
Query query = new Query(criteria);
List<Book> books = mongoTemplate.find(query, Book.class, "book");
}
匹配指定位置元素
// 满足特定索引下条件
// 数组索引从 0 开始,匹配第二项就用 tags.1 作为键
@Test
public void testMatchIndex() {
Criteria criteria = Criteria.where("tags.1").is("编程");
Query query = new Query(criteria);
List<Book> books = mongoTemplate.find(query, Book.class, "book");
}
匹配整个数组
// 元素个数和顺序都要匹配
@Test
public void testArrayMatch() {
Criteria criteria = Criteria.where("tags").is(new String[]{"程序设计", "编程", "python"});
Query query = new Query(criteria);
List<Book> books = mongoTemplate.find(query, Book.class, "book");
}
匹配子文档
@Test
public void testMatchSubDoc() {
Criteria criteria = Criteria.where("authors.name").is("纪涵");
Query query = new Query(criteria);
List<Book> books = mongoTemplate.find(query, Book.class, "book");
}
elementMatch
@Test
public void testElementMatch() {
Criteria criteria = Criteria.where("authors").elemMatch(Criteria.where("name").is("谢希仁").and("sex").is("男"));
Query query = new Query(criteria);
List<Book> books = mongoTemplate.find(query, Book.class, "book");
}
聚合查询
count
@Test
public void testCount() {
Query query = new Query();
long ret = mongoTemplate.count(query, "book");
System.out.println(ret);
}
distinct
@Test
public void testDistinct(){
List<String> distinctPress = mongoTemplate
.findDistinct(new Query(), "press", "book", String.class);
System.out.println(distinctPress);
}
sum、count、avg
@Test
public void testSum() {
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.project("_id", "press", "price"),
Aggregation.group("press")
.count().as("count")
.sum("price").as("total")
.avg("price").as("avg")
);
AggregationResults<Map> aggregationResults = mongoTemplate
.aggregate(aggregation, "book", Map.class);
List<Map> mappedResults = aggregationResults.getMappedResults();
System.out.println(mappedResults);
}
带有表达式的计算
@Test标签:--,Spring,void,criteria,MongoTemplate,query,Criteria,Query,mongoTemplate From: https://blog.51cto.com/knifeedge/5825951
public void testSum2() {
// 定义乘法表达式, 可以是两个字段相乘或者字段与数字相乘
//同理,ArithmeticOperators 包含 加减乘除模等操作
ArithmeticOperators.Multiply price =
ArithmeticOperators.valueOf("PRICE").multiplyBy(5);
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.project("_id", "press", "PRICE"),
Aggregation.group("press")
.count().as("count")
.sum(price).as("total")
.avg(price).as("avg")
);
AggregationResults<Map> aggregationResults = mongoTemplate
.aggregate(aggregation, "book", Map.class);
List<Map> mappedResults = aggregationResults.getMappedResults();
System.out.println(mappedResults);
}