MongoDB and spring integration (3) -- mongorepository for addition, deletion, modification, query, and complex Query

Source: Internet
Author: User
Tags mongodb query

 

Similar to hibernaterepository, by inheriting the mongorepository interface, we can easily add, delete, modify, and query an object. To use the repository function, we must first inherit the mongorepository <t, TD> interface, T is the Bean class stored in the repository, and TD is the unique identifier type of the bean, usually objectid. This interface can be injected into the service, without the need to implement the method in it, spring will automatically generate according to the defined rules.

Example:

 
Public interface personrepository extends mongorepository <person, objectid> {// here you can add additional query methods}

However, mongorepository only implements the most basic addition, deletion, modification, and query functions. To add additional query methods, you can define interface methods according to the following rules. The custom query method in the format of "findby + field name + method suffix". The parameters passed in by the method are the value of the field. In addition, pageable queries are also supported by passing in a pageable object, returns the page set.

Example:

 
Public interface personrepository extends mongorepository <person, objectid> {// query the public page of Data larger than age <product> findbyagegreaterthan (INT age, pageable page );}

The following are supported query types. Each three data types correspond to the following: (method suffix, method example, MongoDB native query statement)

Greaterthan (greater)
Findbyagegreaterthan (INT age)
{"Age": {"$ gt": Age }}

Lessthan (less)
Findbyagelessthan (INT age)
{"Age": {"$ lt": Age }}

Between ()
Findbyagebetween (int from, int)
{"Age": {"$ gt": from, "$ lt": }}

Isnotnull, notnull (whether not empty)
Findbyfirstnamenotnull ()
{"Age": {"$ ne": NULL }}

Isnull, null (null or not)
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)
Findbylocationnear (point)
{"Location": {"$ near": [x, y]}

Within (within the geographic range)
Findbylocationwithin (circle)
{"Location": {"$ within": {"$ Center": [[x, y], distance]}

Within (within the geographic range)
Findbylocationwithin (Box box)
{"Location": {"$ within": {"$ box": [X1, Y1], X2, y2]}

Although the above query functions are already rich, you can use the following method if it cannot meet your needs-based on the query method of the original MongoDB query statement.
For example, add

 
@ Query ("{'name': {'$ RegEx ':? 2, '$ options':' I '}, sales': {' $ gte ':? 1, '$ LTE ':? 2} ") Public page <product> findbynameandagerange (string name, double agefrom, double ageto, pageable page );

The comment query contains the original MongoDB query syntax. we can define the passed query parameters and the parameters of the method through coordinate definition.

You can also specify the data fields to be returned at the end. If you modify the data fields in the preceding example as follows, you can only use the name and age fields in the person table to construct the person object.

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

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.