Query operations are mainly used in two categories: query, Criteria
All of the Find methods require a query object.
1. Find directly through JSON, but this is not recommended in the code.
New Basicquery ("{age: {$lt: $}, Accounts.balance: {$gt: 1000.00}}"); List<Person> result = mongotemplate.find (query, person. Class
2. It is recommended to use the Where + Query method to find.
The Where method generates a criteria object, which can then be incremented by calling different methods (such as Lt,gt,and).
For a more detailed list of operations, please refer to http://docs.spring.io/spring-data/data-mongo/docs/1.5.2.RELEASE/reference/html/mongo.core.html
Import Static Org.springframework.data.mongodb.core.query.Criteria.where; Import Static org.springframework.data.mongodb.core.query.Query.query, ..... List<Person> result = Mongotemplate.find (query (where ("age"). LT (.) and ("accounts.balance "). GT (1000.00d)), person. class
3. MongoDB also supports spatial queries, such as nearby points, just give examples below, see official documentation for details.
@Document (collection= "NewYork") Public classVenue {@IdPrivateString ID; PrivateString name; Private Double[] location; @PersistenceConstructor Venue (String name,Double[] location) { Super(); This. Name =name; This. location =Location ; } PublicVenue (String name,DoubleXDoubley) {Super(); This. Name =name; This. location =New Double[] {x, y}; } PublicString GetName () {returnname; } Public Double[] getLocation () {returnLocation ; } @Override PublicString toString () {return"Venue [id=" + ID + ", name=" + name + ", location=" + arrays.tostring (location) + "]"; } }
Find addresses within a circle
New Circle ( -73.99171, 40.738868, 0.01); List<Venue> venues = template.find (new Query (Criteria.where ("location"). Withincenter (Circle)), Venue. class);
Find addresses within spherical coordinates
New Circle ( -73.99171, 40.738868, 0.003712240453784); List<Venue> venues = template.find (new Query (Criteria.where ("location"). Withincentersphere (Circle)), Venue. class);