Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Fuzzy query and precise query of mongodb operated by Java

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The original intention is to find out how to check the int type like of the mongo database, but it doesn't seem to solve the problem.

Precise query; fuzzy query; paging query, how many pages per page: sort by a field (or rise or fall): the number of queries: greater than, less than, equal to; and, or, a field is not empty, a field does not exist, the query is within a certain range, delete, and so on.

one。 Common queries:

1. Query a piece of data: (it is often used to determine whether there is current data in db when saving, where is matches exactly, while fuzzy matching uses regex...)

Public PageUrl getByUrl (String url) {return findOne (new Query (Criteria.where ("url") .is (url)), PageUrl.class);}

two。 Query multiple pieces of data: linkUrl.id belongs to hierarchical query

Public List getPageUrlsByUrl (int begin, int end,String linkUrlid) {Query query = new Query (); query.addCriteria (Criteria.where ("linkUrl.id") .is (linkUrlid)); return find (query.limit (end-begin) .skip (begin), PageUrl.class);}

3. Fuzzy query:-keyword-regex

Public long getProcessLandLogsCount (List conditions) {Query query = new Query (); if (conditions! = null & & conditions.size () > 0) {for (Condition condition: conditions) {query.addCriteria (Criteria.where (condition.getKey ()) .regex (". *?\" + condition.getValue (). ToString () + ". *);}} return count (query, ProcessLandLog.class);}

At the bottom, the fuzzy query that I have personally practiced in the code only supports the query whose field property is a string. If you check that the field property is an int fuzzy query, there is nothing you can do.

4.gte: greater than or equal to, lte less than or equal to... Note that when querying, the type of each field should be the same as the data type in mongodb.

Public List getProcessLandLogs (int begin,int end,List conditions,String orderField,Direction direction) {Query query = new Query (); if (conditions! = null & & conditions.size () > 0) {for (Condition condition: conditions) {if (condition.getKey (). Equals ("time")) {query.addCriteria (Criteria.where ("time") .gte (condition.getValue () / / gte: greater than or equal to} else if (condition.getKey (). Equals ("insertTime")) {query.addCriteria (Criteria.where ("insertTime") .gte (condition.getValue ());} else {query.addCriteria (Criteria.where (condition.getKey () .is (condition.getValue () }} return find (query.limit (end-begin) .skip (begin) .with (new Sort (new Sort.Order (direction, orderField)), ProcessLandLog.class);} public List getDpsLandsByTime (int begin, int end, Date beginDate,Date endDate) {return find (new Query (Criteria.where ("updateTime") .gte (beginDate) .lte (endDate)) .limit (end-begin) .skip (begin), DpsLand.class);}

Query data that does not exist in the field-keyword-not

Public List getGoodsDetails2 (int begin, int end) {Query query = new Query (); query.addCriteria (Criteria.where ("goodsSummary"). Not (); return find (query.limit (end-begin) .skip (begin), GoodsDetail.class);}

Query data whose field is not empty-keyword-ne

Criteria.where ("key1") .ne (") .ne (null)

Query or statement: a | | b-keyword-orOperator

Criteria criteria = new Criteria (); criteria.orOperator (Criteria.where ("key1") .is ("0"), Criteria.where ("key1") .is (null))

Query and statement: a & & b-keyword-and

Criteria criteria = new Criteria (); criteria.and ("key1") .is (false); criteria.and ("key2") .is (type); Query query = new Query (criteria); long totalCount = this.mongoTemplate.count (query, Xxx.class)

Query the child attributes of an attribute, for example: a statement to look up the key2.keyA of the following data

Var s = {key1: value1, key2: {keyA: valueA, keyB: valueB}}; @ Query ("{'key2.keyA':?0}") List findAllBykeyA (String keyA)

5. Number of queries:-keywords-count

Public long getPageInfosCount (List conditions) {Query query = new Query (); if (conditions! = null & & conditions.size () > 0) {for (Condition condition: conditions) {query.addCriteria (Criteria.where (condition.getKey ()) .is (condition.getValue ();} return count (query, PageInfo.class);}

Find contained in a collection scope:-keyword-in

Criteria criteria = new Criteria (); Object [] o = new Object [] {0,1,2}; / contains all criteria.and ("type") .in (o); Query query = new Query (criteria); query.with (new Sort (Direction.ASC (Direction.ASC, "type")) .with (new Sort (new Sort.Order (Direction.ASC, "title"); List list = this.mongoTemplate.find (query, WidgetMonitor.class)

6. Update a field of a piece of data:

Public WriteResult updateTime (PageUrl pageUrl) {String id = pageUrl.getId (); return updateFirst (new Query (Criteria.where ("id") .is (id)), Update.update ("updateTime", pageUrl.getUpdateTime (), PageUrl.class);}

7. Update multiple fields of a piece of data:

/ / call update private void updateProcessLandLog (ProcessLandLog processLandLog, int crawlResult) {List fields = new ArrayList (); List values = new ArrayList (); fields.add ("state"); fields.add ("result"); fields.add ("time"); values.add ("1"); values.add (crawlResult); values.add (Calendar.getInstance (). GetTime ()); processLandLogReposity.updateProcessLandLog (processLandLog, fields, values) } / / Update public void updateProcessLandLog (ProcessLandLog land, List fields,List values) {Update update = new Update (); int size = fields.size (); for (int I = 0; I < size; I +) {String field = fields.get (I); Object value = values.get (I); update.set (field, value);} updateFirst (new Query (Criteria.where ("id") .is (land.getId ()), update,ProcessLandLog.class);}

8. Delete data:

Public void deleteObject (Class clazz,String id) {remove (new Query (Criteria.where ("id") .is (id)), clazz);}

9. Save the data:

/ / insert one data public void saveObject (Object obj) {insert (obj);} / insert multiple data public void saveObjects (List objects) {for (T t:objects) {insert (t);}}

An example of my own use:

The following example involves:

Precise query: is

Fuzzy query: regex

Paged query, number of pages per page: skip,limit

Sort by a field (up or down): new Sort (new Sort.Order (Sort.Direction.ASC, "port"))

Number of queries: count

Public Map getAppPortDetailByPage (int pageNo, int pageSize, String order, String sortBy, String appPortType, String appPortSeacherName) {Criteria criteria = new Criteria (); if (! appPortType.equals (")) {if (! appPortType.equals (" all ")) {/ / DB field-appmanageType / / the same as port protocol is also a field in the DB table criteria.and (" appmanageType ") .is (appPortType) }} if (! appPortSeacherName.equals ("")) {try {criteria.orOperator (Criteria.where ("port") .is (Integer.parseInt (appPortSeacherName)), Criteria.where ("protocol"). Regex (". *?" + appPortSeacherName + ". *");} catch (Exception e) {criteria.orOperator (Criteria.where ("protocol"). Regex (". *?" + appPortSeacherName + ". *");} Map result = Maps.newHashMap () Query query = new Query (criteria); query.skip ((pageNo-1) * pageSize); query.limit (pageSize); if (order! = null & & sortBy! = null) {query.with (new Sort.Order (order.equals ("asc")? Sort.Direction.ASC: Sort.Direction.DESC, sortBy));} else {query.with (new Sort (new Sort.Order (Sort.Direction.ASC, "port");} List list = this.mongoTemplate.find (query, Appportmanage.class); long count = this.mongoTemplate.count (query, Appportmanage.class); result.put ("datas", list); result.put ("size", count); return result;}

The mongo database is like searching for fields with data types of int

Then I wanted to use a like statement, but I didn't implement it.

Because of my port port storage int property

But on the list page, if you want to support field search, then my port field of int type does not support search.

Then consider that since it is a port, it is a fixed, only

Why support like statements?

If you search for a port number of 1, you will find out the port number of 1, not 1, 11, 21, and so on.

So,

For fields that go to other string types, you use the like statement to search

I'm fine with that.

If you have to implement a like search of int type

I don't know.

Only to change the data structure

If you change int into string,

You can like search.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report