Special operations (with or queries, fuzzy queries, range queries, data sorting, etc.) when connected to MongoDB under the play Framework framework

Source: Internet
Author: User
Tags mongodb mongodb tutorial sort

Existing class information table:

Package models;

Import play.modules.mongo.MongoEntity;
Import Play.modules.mongo.MongoModel;

/**
 * Created by Adinlead on 17/03/04.
 *
/@MongoEntity ("class_msg") public
class Classmsg extends Mongomodel {public
    Integer class;   Class No. public
    Integer number;   School Number public
    String name;   Student name public
    Integer age;

1. With or inquiry 1.1 and query (similar to and in SQL) Nothing to say, this is the most basic 1.2 or query (similar to or in SQL)

You need to load a condition into a query object (com.mongodb.BasicDBObject) or a query list (com.mongodb.BasicDBList) when making or querying
Specific as follows:
Inquiries for all students in class one or "Yang Lo" or the name of "Lin Yu" Students:

Basicdblist params = new basicdblist ();
Params.add (New Basicdbobject ("name", "Yang Lo"));
Params.add (New Basicdbobject ("name", "Lin Yu"));
list<classmsg> resultlist = Classmsg.find ("Byclassand$or", 1,params). Fetch ();
2. Fuzzy query

Regular expressions are required when connecting to MONGO for fuzzy queries
You can use the Java.util.regex.Pattern object here
Specific as follows:
Query Yang surname classmate

Pattern pattern = pattern.compile ("^ Yang *$", pattern.case_insensitive);
list<classmsg> resultlist = Classmsg.find ("byname", pattern). Fetch ();
3. Scope Query

Scope query is relatively simple, only need to use the Com.mongodb.BasicDBObject object
Specific as follows:
Check a class of students older than or equal to 20 years old

/**
greater than use: "$GT"
greater than or equal to use: "$gte" less than use: "
$lt"
less than or equal to use: "$lte"
is not equal to use: "$ne"
other circumstances can go to MongoDB tutorial query
**/
//need to declare the condition first
basicdbobject param = new Basicdbobject ("$gte")
list<classmsg> Resultlist = Mongomodel.find ("Byage", param). Fetch ();
X. Sorting

The play framework's sort operation is done after the input has completed the query criteria before the data is obtained.
Note that because the official documents did not write how the reverse sequence, so I looked up the source code, he is defined as:
If a minus sign ('-') is added before the condition, it is ordered in reverse order, otherwise the default
Specific as follows:
Sort by number of studies

Descending order
list<classmsg> resultlist = Mongomodel.find (). Order ("By-number"). Fetch ();
Positive order
list<classmsg> resultlist = Mongomodel.find (). Order ("Bynumber"). Fetch ();
Query instance:

Check the first class of Yang surname or older than 20 years old classmates, and according to the number of students sorted

Basicdblist params = new basicdblist ();
Pattern pattern = pattern.compile ("^ Yang *$", pattern.case_insensitive);
Params.add (New Basicdbobject ("name", pattern));
Params.add (New Basicdbobject ("Age", New Basicdbobject ("$gte"));
list<classmsg> resultlist = Classmsg.find ("Byclassand$or", 1,params). Order ("Bynumber"). Fetch ();
attached.

Because the query condition in the actual production environment may not be fixed, and Mongomodel does not support the direct incoming parameter list, so I wrote a simple query tool, hope you do not laughed at:

In order to improve your ability (in fact, I am lazy), did not write notes ~

Package utils;

Import Play.modules.mongo.MongoCursor;
Import Java.lang.reflect.Array;
Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;
Import java.util.ArrayList;
Import Java.util.Arrays;

Import java.util.List;
 /** * Created by Adinlead on 16/11/04. */public class Mongoquery {public static mongocursor doquery (class<?> clazz, String querystr, object[] Params
            {try {Object single = Clazz.newinstance ();
            method = Clazz.getdeclaredmethod ("Find", String.class, Array.newinstance (object.class, 0). GetClass ());
        Return (Mongocursor) Method.invoke (single, New Object[]{querystr, params});
        } catch (Exception e) {return null; }} public static Mongocursor doquery (class<?> clazz, String querystr, List params) {return Doquer
    Y (Clazz, Querystr, Params.toarray ()); } public static Mongocursor doquery (class<?> clazz, StringBuilder querYstr, List params) {return doquery (Clazz, querystr.tostring (), Params.toarray ()); } public static Long Docount (class<?> clazz, String querystr, object[] params) {try {Obje
            CT single = Clazz.newinstance ();
            method = Clazz.getdeclaredmethod ("Count", String.class, Array.newinstance (object.class, 0). GetClass ());
        Return (Long) Method.invoke (single, New Object[]{querystr, params});
        } catch (Exception e) {return null; }} public static Long Docount (class<?> clazz, String querystr, List params) {return Docount (clazz
    , Querystr, Params.toarray ()); } public static Long Docount (class<?> clazz, StringBuilder querystr, List params) {return Docount (Claz
    Z, querystr.tostring (), Params.toarray ()); }
}
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.