Various operations of MongoDB in Java

Source: Internet
Author: User
Tags int size

A. Common query:

1. Query a data: (more used to determine whether the DB has the current data, here is exactly match, fuzzy match using regex ...)

 Public pageurl getbyurl (String url) {        return findOne (new Query (criteria.where ("url"). Is (URL )), Pageurl. class );    }

2. Query multiple data: Linkurl.id belongs to the hierarchical query

 Public List<pageurl> getpageurlsbyurl (intint  end,string linkurlid) {                new Query ();        Query.addcriteria (Criteria.where ("Linkurl.id"). Is (Linkurlid));         return Find (Query.limit (end-begin). Skip (Begin), Pageurl.  Class);            }

3. Fuzzy query:

 Public LongGetprocesslandlogscount (list<condition>conditions) {Query Query=NewQuery (); if(Conditions! =NULL&& conditions.size () > 0) {             for(Condition condition:conditions) {Query.addcriteria (Criteria.where (Condition.getkey ()). Regex (". *?\\ "+condition.getvalue (). toString () +". * ");
} } returnCount (query, Processlandlog.class); }


4. GTE: Greater than or equal to, LTE less than equals ... Note that the type of each field is consistent with the data type in MongoDB when querying

 PublicList<processlandlog> Getprocesslandlogs (intBeginintEnd,list<condition>conditions,string orderfield,direction Direction) {Query Query=NewQuery (); 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 () ));                }            }        }        returnFind (Query.limit (end-begin). Skip (Begin). with (NewSort (NewSort.order (direction, OrderField)), Processlandlog.class); }

Public list<dpsland> 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);
}

5. Number of queries:

 Public long getpageinfoscount (list<condition> conditions) {        new  Query ();         if null && conditions.size () > 0) {            for  (Condition Condition: Conditions) {
Query.addcriteria (Criteria.where (Condition.getkey ()). is (Condition.getvalue ())); } } return count (query, PageInfo. Class); }

6. Update a field for one data:

Public Writeresult UpdateTime (pageurl pageurl) {        String id = Pageurl.getid ();        return Updatefirst (new Query (Criteria.where ("id")), Update.update ("UpdateTime", Pageurl.getupdatetime ()), Pageurl. class);}     

7. Update multiple fields of one data:

//Call UpdatePrivatevoidUpdateprocesslandlog (Processlandlog Processlandlog,IntCrawlresult) {list<string> fields =New arraylist<string>(); list<object> values =New arraylist<object>(); 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<string> fields,list<object> new update (); int size = Fields.size (); for (int i = 0; i < size; I++ 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<t> clazz,string ID) {        remove (new Query (Criteria.where ( "id"). is (ID)), clazz);    }

9. Save the data:

Insert a piece of data
publicvoid saveobject (Object obj) { insert (obj); }
Insert multiple Data public void saveobjects (list<t> objects) { for (t t:objects) { insert (t); } }


 

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.