MongoDB's Tool class

Source: Internet
Author: User
Tags mongoclient mongodb

Package Com.guilf.service.impl;import Java.util.arraylist;import Java.util.date;import java.util.List;import Java.util.regex.Pattern; Import Org.apache.commons.lang.stringutils;import org.bson.Document; Import Com.alibaba.fastjson.jsonobject;import Com.github.walker.mybatis.paginator.pagelist;import Com.mongodb.basicdbobject;import Com.mongodb.db;import Com.mongodb.dbcollection;import com.mongodb.DBCursor; Import Com.mongodb.mongo;import Com.mongodb.mongoclient;import Com.mongodb.mongoclientoptions;import Com.mongodb.mongocredential;import Com.mongodb.serveraddress;import Com.mongodb.client.finditerable;import Com.mongodb.client.mongocollection;import Com.mongodb.client.mongocursor;import Com.mongodb.client.MongoDatabase  ;        public class Mongodbutil {private static mongoclient client = NULL;      Private Mongodbutil () {} static {initdbprompties (); }/** * Initialize connection pool */private static void Initdbprompties () {StriNg Url=propertyholder.getproperty ("Mongodb.url");      String dbname=propertyholder.getproperty ("Mongodb.dbname");     String username=propertyholder.getproperty ("Mongodb.username");     String password=propertyholder.getproperty ("Mongodb.password");     int connectionsperhost = integer.valueof (Propertyholder.getproperty ("Mongodb.connectionsperhost"));     int threads = integer.valueof (Propertyholder.getproperty ("mongodb.threads"));     int maxwaittime=integer.valueof (Propertyholder.getproperty ("Mongodb.maxwaittime"));     int sockettimeout=integer.valueof (Propertyholder.getproperty ("mongodb.sockettimeout"));     int maxconnectionlifetime=integer.valueof (Propertyholder.getproperty ("Mongodb.maxconnectionlifetime"));          int connecttimeout = integer.valueof (Propertyholder.getproperty ("mongodb.connecttimeout"));     List<mongocredential> credentials = new arraylist<mongocredential> ();     ServerAddress address = new serveraddress (URL); Mongocredential credential = MonGocredential.createcredential (Username,dbname,password.tochararray ());          Credentials.add (credential);                    Mongoclientoptions.builder build = new Mongoclientoptions.builder ();         Build.connectionsperhost (Connectionsperhost);         Build.maxwaittime (Maxwaittime);         Build.maxconnectionlifetime (Maxconnectionlifetime);         Build.connecttimeout (ConnectTimeout);         Build.threadsallowedtoblockforconnectionmultiplier (threads);         Build.sockettimeout (sockettimeout);           Mongoclientoptions options = Build.build ();      Client = new Mongoclient (address, credentials, options);          }/** * Get database * @param dbName database * @return */public static mongodatabase getdb (String dbName) {    Return Client.getdatabase (DbName); /** * Get table * @param dbName database * @param collectionname Collection * @return */public static MONGOC Ollection<document> getcollection (String dbname,string collectionname) {    Mongocollection<document> collection = Getdb (DbName). GetCollection (CollectionName);    return collection;  }/** * Insert table data * @param dbName database * @param collectionname Collection * @param json to library JSON */public static void Insert (String dbname,string collectionname,string json) {mongocollection<document> collection =        GetCollection (DbName, CollectionName);    Document document = Document.parse (JSON);    Collection.insertone (document); /** * Paging Query user action log * @param dbName database * @param collectionname Collection * @param acctno account number * @param Start * @param pageSize * @return */public static pagelist<useroplog> Finduseroplog (String dbname,s Tring collectionname,string acctno,string tenantid,string keyword,string startdate,string endDate,int start,int    pageSize) {list<useroplog> loglist = new arraylist<useroplog> (); Mongocollection<document> collection = GetCollection (DbnaMe, CollectionName);        Basicdbobject queryobject = new Basicdbobject ();        Basicdbobject tmpobject = new Basicdbobject ();               Basicdbobject dateobject = new Basicdbobject ();                if (Stringutils.isnotempty (Acctno)) {queryobject.put ("Acctno", Acctno);                } if (Tenantid!=null) {queryobject.put ("tenantId", tenantId); } if (Stringutils.isnotempty (KeyWord)) {Pattern pattern = pattern.compile ("^.*" +keyword+ ". *$", Pat Tern.        case_insensitive);                Queryobject.put ("Opdesc", pattern); } tmpobject.put ("$gte", StartDate); Greater than Dateobject = Tmpobject.append ("$lte", endDate);//Less than queryobject.put ("OpTim              E ", dateobject);               Finditerable<document> iterator= Collection.find (queryobject). Sort (New Basicdbobject ("OpTime",-1));        int count = 0; Mongocursor<document> cursor= iteRator.iterator ();        while (Cursor.hasnext ()) {Document doc = Cursor.next (); if (Count>=start && count<=pagesize+start-1) {Useroplog Useroplog = new Userop                Log ();                Useroplog.setacctno (doc.getstring ("Acctno"));                Useroplog.setclasz (doc.getstring ("Clasz"));                Useroplog.seterrormsg (doc.getstring ("errormsg"));                Useroplog.setmethod (Doc.getstring ("method"));                Useroplog.setname (doc.getstring ("name"));                Useroplog.setopdesc (doc.getstring ("Opdesc"));                Useroplog.setopresult (Doc.getinteger ("Opresult"));                Useroplog.setoptime (doc.getstring ("OpTime"));                Useroplog.seturi (doc.getstring ("uri"));                Useroplog.settenantid (doc.getstring ("tenantId"));        Loglist.add (Useroplog);            } count++;        } cursor.close (); pagelist<useroplog> pagelist = new PageliSt<useroplog> (Loglist,count);            return pagelist; /** * Paged Query interface Call log * @param dbName database * @param collectionname Collection * @param tenantId merchant ID * @par      AM appId App ID * @param startdate start date * @param endDate End Date * @param start * @param pageSize * @return */public static pagelist<usercalllog> Findusercalllog (String dbname,string collectionname,string TenantId,S Tring appid,string startdate,string enddate,int start,int pageSize) {list<usercalllog> loglist = new Array    List<usercalllog> ();        Mongocollection<document> collection = GetCollection (DbName, CollectionName);        Basicdbobject queryobject = new Basicdbobject ();        Basicdbobject tmpobject = new Basicdbobject ();               Basicdbobject dateobject = new Basicdbobject ();                if (Stringutils.isnotempty (tenantId)) {queryobject.put ("tenantId", tenantId); } if (Stringutils.isnotempty (appId)) {queryobject.put ("appId", appId); } tmpobject.put ("$gte", StartDate); Greater than Dateobject = Tmpobject.append ("$lte", endDate);//Less than queryobject.put ("Reqti              Me ", dateobject);               Finditerable<document> iterator= Collection.find (queryobject);        int count = 0;        Mongocursor<document> cursor= iterator.iterator ();        while (Cursor.hasnext ()) {Document doc = Cursor.next (); if (Count>=start && count<=pagesize+start-1) {Usercalllog Usercalllog = new Usercalllo        g ();                Usercalllog.setappid (doc.getstring ("appId"));                Usercalllog.setclienthost (doc.getstring ("ClientHost"));                Usercalllog.setclientip (doc.getstring ("ClientIP"));                Usercalllog.setclientport (Doc.getinteger ("ClientPort"));                Usercalllog.seterrorcode (doc.getstring ("ErrorCode")); UsercallLog.seterrormsg (doc.getstring ("errormsg"));                Usercalllog.setflowno (doc.getstring ("Flowno"));                Usercalllog.setinterfaceclasz (doc.getstring ("Interfaceclasz"));                Usercalllog.setinterfaceid (doc.getstring ("InterfaceID"));                Usercalllog.setmethodid (doc.getstring ("Methodid"));                Usercalllog.setmethodname (doc.getstring ("MethodName"));                Usercalllog.setreqbytes (Doc.getinteger ("reqbytes"));                Usercalllog.setreqtime (doc.getstring ("Reqtime"));                Usercalllog.setresbytes (Doc.getinteger ("resbytes"));                Usercalllog.setrestime (doc.getstring ("Restime"));                Usercalllog.setsvcid (doc.getstring ("Svcid"));                Usercalllog.setsvcinterface (doc.getstring ("Svcinterface"));                Usercalllog.settenantid (doc.getstring ("tenantId"));                Usercalllog.settoken (doc.getstring ("token"));                Usercalllog.seturi (doc.getstring ("uri")); LOglist.add (Usercalllog);            } count++;        } cursor.close ();        pagelist<usercalllog> pagelist = new pagelist<usercalllog> (loglist,count);            return pagelist; }       }

  

MongoDB's Tool class

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.