Get the App
SLTechnology News&Howtos  ›  Database  › 

[MongoDB] spring-data-mongo configuration

Shulou Source: shulou.com Published: 2022-06-01 04:17:20 10月04日 Update

Config.properties

# mongodb settingmongo.host=127.0.0.1mongo.port=27017mongo.connectionsPerHost=100mongo.threadsAllowedToBlockForConnectionMultiplier=50mongo.connectTimeout=1000mongo.maxWaitTime=1500mongo.autoConnectRetry=truemongo.socketKeepAlive=truemongo.socketTimeout=0mongo.slaveOk=true

Spring_mongo.xml

Entity

@ Document (collection = "user") public class User {@ Id private int id; private String name; private int age; private String city; private String mote; public int getId () {return id;} public void setId (int id) {this.id = id } public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age } public String getCity () {return city;} public void setCity (String city) {this.city = city;} public String getMote () {return mote;} public void setMote (String mote) {this.mote = mote;}}

Dao configuration

Strict compliance with public interface MGUserDao extends MongoRepository {/ * method names * @ param age * @ param page * @ return * / public Page findByAgeGreaterThan (int age, Pageable page) / * * @ Query declares that the query does not require strict compliance with the rule * @ param name * @ param ageFrom * @ param ageTo * @ param pageable * @ return * / @ Query ("{'name': {' $regex':?0}, 'age': {' $gte':?1] '$lte':?2}} ") public Page findByNameAndAgeRange (String name, int ageFrom, int ageTo, Pageable pageable) @ Query ("{? 0String value 1}") public List findByAttribute (String key, String value);}

Use of dao layer

MongoRepository only implements the most basic functions of adding, deleting, changing and querying. To add additional query methods, you can define the method of the interface according to the following rules. Custom query method in the format of "findBy+ field name + method suffix". The parameter passed in by the method is the value of the field. In addition, it also supports paging query, which returns the Page collection by passing in a Pageable object. For example:

/ / query data larger than age public Page 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 (greater than) findByAgeGreaterThan (int age) {"age": {"$gt": age}} LessThan (less than) findByAgeLessThan (int age) {"age": {"$lt": age}} Between (in. Between) findByAgeBetween (int from, int to) {"age": {"$gt": from, "$lt": to}} IsNotNull, NotNull (not empty) findByFirstnameNotNull () {"age": {"$ne": null}} IsNull Null (empty) findByFirstnameNull () {"age": null} Like (Fuzzy query) findByFirstnameLike (String name) {"age": age} (age as regex) (No keyword) findByFirstname (String name) {"age": name} Not (not included) findByFirstnameNot (String name) {"age": {"$ne": name}} Near (query geographically similar) findByLocationNear (Point point) {"location": {"$near": [x Y]}} Within (within geographical range) findByLocationWithin (Circle circle) {"location": {"$within": {"$center": [x, y], distance]} Within (within geographical range) findByLocationWithin (Box box) {"location": {"$within": {"$box": [[x1, y1], x2, y2]}

Although the above query features are already very rich, but if you can not meet the usage, you can use the method-based on the query method of the original query statement of mongodb.

Example 1: add to the original interface, comment Query is the original query syntax of mongodb, we can define the query parameters passed in, and define the parameters of the method through coordinates.

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

Example 2: you can also specify the data fields to be returned later. If the above example is modified as follows, only the name and age fields in the person table will be used to build the person object.

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

Tags: Queries methods fields locations parameters geography geographic locations data examples functions suffixes objects interfaces ranges statements facets support configuration proximity between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux NVidia Shulou Technology macOS MySQL