springboot mongodb jpa常用方法整理

Posted zincredible

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot mongodb jpa常用方法整理相关的知识,希望对你有一定的参考价值。

官方文档https://docs.spring.io/spring-data/data-mongodb/docs/current/reference/html/index.html

查询:

***************************************************************************

mongoTemple查询和修改

@Autowired
MongoTemplate mongoTemp;

Criteria criteria = Criteria.where("name").regex("www").andOperator(Criteria.where("creatTime").gte(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2018-06-20 21:48:54")));
mongoTemp.find(new Query(criteria), Movie.class);

**************************************************************************

 

List<User> findByName(String name);

List<User> findByNameAndAge(String name,String age);

long countByName(String name);

原生山查询语句查询

@Query(value="{‘$and‘:[{‘name‘:{‘$regex‘:?0}}]}")
public List<Movie> findByName(String name);

 

KeywordSampleLogical result

After

findByBirthdateAfter(Date date)

{"birthdate" : {"$gt" : date}}

GreaterThan

findByAgeGreaterThan(int age)

{"age" : {"$gt" : age}}

GreaterThanEqual

findByAgeGreaterThanEqual(int age)

{"age" : {"$gte" : age}}

Before

findByBirthdateBefore(Date date)

{"birthdate" : {"$lt" : date}}

LessThan

findByAgeLessThan(int age)

{"age" : {"$lt" : age}}

LessThanEqual

findByAgeLessThanEqual(int age)

{"age" : {"$lte" : age}}

Between

findByAgeBetween(int from, int to)

{"age" : {"$gt" : from, "$lt" : to}}

In

findByAgeIn(Collection ages)

{"age" : {"$in" : [ages…?]}}

NotIn

findByAgeNotIn(Collection ages)

{"age" : {"$nin" : [ages…?]}}

IsNotNullNotNull

findByFirstnameNotNull()

{"firstname" : {"$ne" : null}}

IsNullNull

findByFirstnameNull()

{"firstname" : null}

LikeStartingWithEndingWith

findByFirstnameLike(String name)

{"firstname" : name} (name as regex)

NotLikeIsNotLike

findByFirstnameNotLike(String name)

{"firstname" : { "$not" : name }} (name as regex)

Containing on String

findByFirstnameContaining(String name)

{"firstname" : name} (name as regex)

NotContaining on String

findByFirstnameNotContaining(String name)

{"firstname" : { "$not" : name}} (name as regex)

Containing on Collection

findByAddressesContaining(Address address)

{"addresses" : { "$in" : address}}

NotContaining on Collection

findByAddressesNotContaining(Address address)

{"addresses" : { "$not" : { "$in" : address}}}

Regex

findByFirstnameRegex(String firstname)

{"firstname" : {"$regex" : firstname }}

(No keyword)

findByFirstname(String name)

{"firstname" : name}

Not

findByFirstnameNot(String name)

{"firstname" : {"$ne" : name}}

Near

findByLocationNear(Point point)

{"location" : {"$near" : [x,y]}}

Near

findByLocationNear(Point point, Distance max)

{"location" : {"$near" : [x,y], "$maxDistance" : max}}

Near

findByLocationNear(Point point, Distance min, Distance max)

{"location" : {"$near" : [x,y], "$minDistance" : min, "$maxDistance" : max}}

Within

findByLocationWithin(Circle circle)

{"location" : {"$geoWithin" : {"$center" : [ [x, y], distance]}}}

Within

findByLocationWithin(Box box)

{"location" : {"$geoWithin" : {"$box" : [ [x1, y1], x2, y2]}}}

IsTrueTrue

findByActiveIsTrue()

{"active" : true}

IsFalseFalse

findByActiveIsFalse()

{"active" : false}

Exists

findByLocationExists(boolean exists)

{"location" : {"$exists" : exists }}

KeyWord可以用and方法连起来。

例如:

List<DiscountCode> findFirst5ByActivityIdInAndEndTimeAfterAndStatus(List<ObjectId> activityIds, Date endTime,String status);

 

 

删除:

List <Person> deleteByLastname(String lastname);

Long deletePersonByLastname(String lastname);





以上是关于springboot mongodb jpa常用方法整理的主要内容,如果未能解决你的问题,请参考以下文章

你应该知道的springboot mongodb jpa那些小tip.

你应该知道的springboot mongodb jpa那些小tip.

springboot jpa mongodb 多条件分页查询

Spring Boot MongoDB JPA 简化开发

无法将 Spring Data MongoDB + Spring Data JPA 与 Spring Boot 一起使用

SpringBoot JPA实现增删改查分页排序事务操作等功能