Spring Data MongoDB查询

(一)使用Query和Criteria类

假设数据库状态如下:

[{"_id" : ObjectId("5b9a10d25ef6a842e429df6e"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "zhangsan",

"age" : "30"},

{"_id" : ObjectId("5b9a244d5ef6a8ae118f1fc4"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "lisi",

"age" : "25"},

{"_id" : ObjectId("5b9f74485ef6a831080b3d19"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "wangwu",

"age" : "20"}]

1、is查询

Query query = new Query();

query.addCriteria(Criteria.where("name").is("zhangsan"));

User user = mongoTemplate.find(query, User.class);

结果返回:

{"_id" : ObjectId("5b9a10d25ef6a842e429df6e"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "zhangsan",

"age" : "30"}

2、正则查询

Query query = new Query();

query.addCriteria(Criteria.where("name").regex("^w"));

User user = mongoTemplate.find(query, User.class);

结果返回:

{"_id" : ObjectId("5b9f74485ef6a831080b3d19"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "wangwu",

"age" : "20"}

3、lt和gt

lt(小于)、gt(大于)、lte(小于等于)、gte(大于等于)

Query query = new Query();

query.addCriteria(Criteria.where("age").lt("35").gt("20"));

List user = mongoTemplate.find(query, User.class);

结果返回:

[{"_id" : ObjectId("5b9a10d25ef6a842e429df6e"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "zhangsan",

"age" : "30"},

{"_id" : ObjectId("5b9a244d5ef6a8ae118f1fc4"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "lisi",

"age" : "25"}]

4、结果排序

Query query = new Query();

query.with(new Sort(Sort.Direction.ASC, "age"));

List user = mongoTemplate.find(query, User.class);

结果返回:

[{"_id" : ObjectId("5b9f74485ef6a831080b3d19"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "wangwu",

"age" : "20"},

{"_id" : ObjectId("5b9a244d5ef6a8ae118f1fc4"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "lisi",

"age" : "25"},

{"_id" : ObjectId("5b9a10d25ef6a842e429df6e"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "zhangsan",

"age" : "30"}]

5、分页

Query query = new Query();

PageRequest pageRequest = PageReqest.of(0, 2);

query.with(pageRequest);

结果返回两页大小为2的页面。

(二)生成的查询方法

需要在接口上继承MongoRepository,如下:

public interface UserRepository extends MongoRepository {

......

}

与MongoRepository相关接口


Spring Data MongoDB查询_第1张图片
Spring Data MongoDB查询_第2张图片
Spring Data MongoDB查询_第3张图片

1、通过FindBy查询

使用FindBy通过entity实体属性查询

User findByName(String name);

User user = userRepository.findByName("zhangsan");得到跟1.1一样的结果

2、StartingWith和EndingWith

List findByNameStartingWith(String regex);

List findByNameEndingWith(String regex);

List user = userRepository.findByNameStartingWith("z");

List user = userRepository.findByNameEndingWith("n");

两个查询得到一样的结果

{"_id" : ObjectId("5b9a10d25ef6a842e429df6e"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "zhangsan",

"age" : "30"}

3、Between

List findByAgeBetween(int gt, int lt);

List user = userRepository.findByAgeBetween(20, 35);得到跟1.3一样的结果

4、Like模糊查询

List findByNameLike(String name);

List user = userRepository.findByNameLike("n");

结果返回:

[{"_id" : ObjectId("5b9a10d25ef6a842e429df6e"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "zhangsan",

"age" : "30"},

{"_id" : ObjectId("5b9f74485ef6a831080b3d19"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "wangwu",

"age" : "20"}]

5、OrderBy排序

在2.4模糊查询上按年龄递增排序

List findByNameLikeOrderByAgeAsc(String name);

List user = userRepository.findByNameLikeOrderByAgeAsc("n");

结果返回:

[{"_id" : ObjectId("5b9f74485ef6a831080b3d19"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "wangwu",

"age" : "20"},

{"_id" : ObjectId("5b9a10d25ef6a842e429df6e"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "zhangsan",

"age" : "30"}]

6、使用Top和First限制查询的结果大小

User findTopByOrderByAgeDesc();

User user = userRepository.findTopByOrderByAgeDesc();

结果返回:

{"_id" : ObjectId("5b9a10d25ef6a842e429df6e"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "zhangsan",

"age" : "30"}

List findFirst2ById(String id);

List user = userRepository.findFirst2ById(String id);

结果返回:

[{"_id" : ObjectId("5b9a10d25ef6a842e429df6e"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "zhangsan",

"age" : "30"},

{"_id" : ObjectId("5b9a244d5ef6a8ae118f1fc4"),

"_class" : "com.ceshi.mongo.entity.User",

"name" : "lisi",

"age" : "25"}]

7、分页

Page findAll(PageAble pageable);

Page page = userRepository.findAll(PageRequest.of(0, 2));

(三)使用@Query注解

1、对2.1使用@Query注解查询

@Query("{  `name` : ?0 }")

User findByName(String name);

占位符?0引用方法的第一个参数。

2、正则表达式

对2.4使用@Query注解查询

@Query("{ `name` : { $regex : ?0 }}")

List findByRegexName(String regex);

3、对2.3使用@Query注解查询

@Query("{ `age` : { $gt : ?0 , $lt : ?1 }}")

List findByAgeBetween(int gt, int lt);

4、使用查询语法

@Query(value = "select * from User u where u.age>?1 and u.age

List findByAgeBetween(int gt, int lt);

like表达式

@Query(value = "select * from User u where u.name like %:name%")

Lise findByName(@Param("name") String name);

使用@Param注解注入参数

@Query(value = "select * from User u where u.name = :name and u.age = :age")

Lise findByNamedParam(@Param("name") String name, @Param("age") Int age);

你可能感兴趣的:(Spring Data MongoDB查询)