美文网首页
Spring Data MongoDB查询

Spring Data MongoDB查询

作者: Sylvia_Nie | 来源:发表于2018-11-30 10:41 被阅读0次

(一)使用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> 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> 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<User, String> {

......

}

与MongoRepository相关接口

1、通过FindBy查询

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

User findByName(String name);

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

2、StartingWith和EndingWith

List<User> findByNameStartingWith(String regex);

List<User> findByNameEndingWith(String regex);

List<User> user = userRepository.findByNameStartingWith("z");

List<User> user = userRepository.findByNameEndingWith("n");

两个查询得到一样的结果

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

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

"name" : "zhangsan",

"age" : "30"}

3、Between

List<User> findByAgeBetween(int gt, int lt);

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

4、Like模糊查询

List<User> findByNameLike(String name);

List<User> 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<User> findByNameLikeOrderByAgeAsc(String name);

List<User> 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<User> findFirst2ById(String id);

List<User> 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<User> findAll(PageAble pageable);

Page<User> 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<User> findByRegexName(String regex);

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

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

List<User> findByAgeBetween(int gt, int lt);

4、使用查询语法

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

List<User> findByAgeBetween(int gt, int lt);

like表达式

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

Lise<User> findByName(@Param("name") String name);

使用@Param注解注入参数

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

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

相关文章

网友评论

      本文标题:Spring Data MongoDB查询

      本文链接:https://www.haomeiwen.com/subject/blvccqtx.html