[Spring Data MongoDB] Learning Note--mongotemplate query operation

Source: Internet
Author: User

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);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.