Mongodb integrates with spring MongoRepository to implement addition, deletion, modification and complex query

Similar to HibernateRepository, by inheriting the MongoRepository interface, we can easily add, delete, modify and check an object. To use the Repository function, first inherit the MongoRepository interface, where T is the bean class saved in the warehouse, and TD is the unique identifier of the bean The type, usually ObjectId. After injecting the interface into the service, it can be used without implementing the methods in it, and spring will automatically generate it according to the defined rules.

example:

public interface PersonRepository extends   
  
MongoRepository<Person, ObjectId>{  
//additional query methods can be added here   

 However, MongoRepository only implements the most basic functions of adding, deleting, modifying and checking. To add additional query methods, you can define interface methods according to the following rules. Custom query method, the format is "findBy+field name+method suffix", the parameter passed in the method is the value of the field, and also supports paging query, by passing in a Pageable object, returns the Page collection.

 

Example :

 

public interface PersonRepository extends   
  
MongoRepository<Person, ObjectId>{  
 //Query data greater than age    
       public Page<Product> findByAgeGreaterThan(int age,Pageable page) ;  

 The following are the supported query types, corresponding to every three pieces of data: (method suffix, method example, mongodb native query statement)

 

GreaterThan(大于) 
findByAgeGreaterThan(int age) 
{"age" : {"$gt" : age}}

LessThan(小于) 
findByAgeLessThan(int age) 
{"age" : {"$lt" : age}}

Between(在...之间) 
findByAgeBetween(int from, int to) 
{"age" : {"$gt" : from, "$lt" : to}}

IsNotNull, NotNull (is not empty) 
findByFirstnameNotNull() 
{"age" : {"$ne" : null}}

IsNull, Null (is it empty) 
findByFirstnameNull() 
{"age" : null}

Like(模糊查询) 
findByFirstnameLike(String name) 
{"age" : age} ( age as regex)

(No keyword) findByFirstname(String name) 
{"age" : name}

Not(不包含) 
findByFirstnameNot(String name) 
{"age" : {"$ne" : name}}

Near (query geographically close) 
findByLocationNear(Point point) 
{"location" : {"$near" : [x,y]}}

Within 
findByLocationWithin(Circle circle) 
{"location" : {"$within" : {"$center" : [ [x, y], distance]}}}

Within 
findByLocationWithin(Box box) 
{"location" : {"$within" : {"$box" : [ [x1, y1], x2, y2]}}}

Although the above query functions are already very rich, if the usage situation cannot be satisfied, you can use the following method---the query method based on the original query statement of mongodb.
Example: adding to the original interface

 

@Query("{ 'name':{'$regex':?2,'$options':'i'}, sales': {'$gte':?1,'$lte':?2}}")  
public Page<Product> findByNameAndAgeRange(String name,double ageFrom,double ageTo,Pageable page);  

 Note that Query contains the original query syntax of mongodb. We can define the query parameters passed in, and define the parameters of the method through coordinates.

 

You can also specify the data fields to be returned later. For example, the above example is modified as follows, and the person object is constructed only through the name and age fields in the person table. 

 

@Query(value="{ 'name':{'$regex':?2,'$options':'i'}, sales':{'$gte':?1,'$lte':?2}}",fields="{ 'name' : 1, 'age' : 1}")   
public Page<Product> findByNameAndAgeRange(String name,double ageFrom,double ageTo,Pageable page);

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326420148&siteId=291194637