Searching

Before each lookup, you need to use “query.use (collection name)” to declare the collection you want to look up. If no deserialization is used, all lookups will return Json list or JSON object by default.

Search by ID

query.use("item").byId("5710a81ab73a87092e17a02b").find()

Find All

query.use("item").findAll()

Search by Conditions

query.use("item").eq("b","2").find()

(ps:MongoDB can distinguish text and numbers, so pay attention to setting conditions)

Deserialization lookup results (findAll and pagination query are also supported)

query.use("item").eq("b","2").find(xx.class)

Get Number of Results

query.use("item").eq("b","2").count()

Conditions

Support the following conditions followed by Use:

  • in() Include, if key is _id, it will automatically convert text ID to ObjectId that mongodb required.
  • eq() Equal
  • ne() Not Equal
  • gt() Greater than
  • lt() Less than
  • gte() Equal or Greater than
  • lte() Equal or Less than
  • like() Fuzzy search, “like()” not only support “in()”, but also support lookups what begin with or end in.
  • like(int type,String key, String value) 1(int type) is what start with and other figures except I is what end in.

For more conditions, you can use “query.filter ()” to load native conditions such as “query.filter (Filters.in (key, value))”, which the “filter” is from mongodb drive.

If you want to get more conditions, see: http://mongodb.github.io/mongo-java-driver/3.2/builders/filters/

All conditions are connected by “and”, so multiple conditions such as update, search, delete can be connected by this way. For example, if I would like to find a boy less than 18 years old and his name contains the word "Chen" :

query.lt("age",18).eq("sex","man").like("name","陈").find()  

If you want to use “or” or “nor”:


query.use("teacher").or(new MongoQuery().eq("name","卫越相"),new MongoQuery().eq("name","危兴修")).find();

//Conditions in “or”

Add join features in 1.0.7 version. If you insert by “join (collection’s field name, collection name need to be associated , ObjectId need to be associated)”, you can find it by “join ("field name of this collection")”. Note that “join” only supports single ObjectId. Do like MySQL join can refer to aggregation operation.

Sorting and Quantity

“find” could support sorting and quantity, there are two examples below.

query.lt("age",18). //the following methods:   

 .ascending(Ascending condition 1,Ascending condition 2....) 
 .descending(descending condition 1,descending condition 2...)
 .limit(limit number of results)     
 .projection(only return column 1,only return column 2...)
 .skip(Skip specified number of lines)

Sorting conditions require the use of native driver writing. For details, see:

http://mongodb.github.io/mongo-java-driver/3.2/builders/sorts/

Of course if you just want one result you can use “findOne” instead of “find” such as “search by ID”. If you want to know whether a result exist, replace “find ()” to “exist ()” .


query.byId("id").findOne();

Other common lookups:

“max (field name)” is to find the largest one and “min (field name)” is to find the smallest one. In addition, “exist (field name)” only find the existence of this field name.

Pagination

The usage of Pagination is very simple which just need to create a new MongoPaginate object and put in query object that writes query conditions , the number of rows per page and the current number of pages.


MongoPaginate page=new MongoPaginate(new MongoQuery().use("student").descending("age").join("teacher"),the number of rows per page, the current number of pages)

page.findAll(Student.class); // Ignore conditions to find all

page.find();//Search by conditions

Don’t use “limit” and “skip” conditions in the put-in query object, so as not to affect the query results.

results matching ""

    No results matching ""