Manipulating MongoDB in Java

Source: Internet
Author: User
Tags mongoclient

MongoDB is a NoSQL database that stores something like a JSON object that supports some operations similar to a relational database. Here is the Java operation MongoDB.

First, establish a connection (without using any framework, the connection is written by itself, while in this class write the method of closing the connection), the incoming IP address, port and database name (MongoDB called collection)

 Public classMongodbconn {Private StaticMongoclient mongoclient =NULL; Private StaticMongodatabase mongodatabase =NULL;  Public StaticMongodatabase Getdatabase (String IPAddress,intport,string DbName) {mongoclient=Newmongoclient (IPAddress, Port); Mongodatabase=mongoclient.getdatabase (dbName); returnmongodatabase; }     Public Static voidcloseDatabase () {Try{            if(mongoclient!=NULL) {mongoclient.close (); }        }Catch(Exception e) {Throw Newruntimeexception (); }finally{mongoclient=NULL; }                }}

Second, the use of operations, the following is an example of the query

 PublicList<data>Getallabsdata (Rankparam rankparam) {List<Data> dataList =NewArraylist<data>(); Mongodatabase Mongodatabase= Mongodbconn.getdatabase ("localhost", 27017, "Area_new"); Mongocollection<Document> collection = Mongodatabase.getcollection ("vaabs_monthly"); Finditerable<Document> finditerable =NULL; if("Vaabs". Equals (Rankparam.getindexcode ())) {            if(Rankparam.getareacode () = =NULL|| "". Equals (Rankparam.getareacode ())) {//National//Db.vaabs_monthly.find ({"$and": [{"Indusid": 300},{"AreaCode":/.*0000$/}]})Pattern Querypattern = Pattern.compile (". *0000$"), pattern.case_insensitive); Basicdbobject Queryobject=NewBasicdbobject (); Queryobject.put ("AreaCode", Querypattern); Queryobject.put ("Indusid", Rankparam.getindusid ()); Finditerable=Collection.find (Queryobject); }Else{//data for all municipalities in the province//Db.vaabs_monthly.find ({"$and": [{"Indusid": 300},{"date": "2007-01-31"},{"AreaCode":/^13/},{"AreaCode":/.*00$ /},{"AreaCode": {"$not":/.*0000$/}]})//Pattern Startpattern = Pattern.compile ("^13", pattern.case_insensitive);Pattern Startpattern = pattern.compile ("^" +Rankparam.getareacode (), pattern.case_insensitive); Pattern Endpattern= Pattern.compile (". *00$", pattern.case_insensitive); Pattern Notpattern= Pattern.compile (". *0000$", pattern.case_insensitive); Basicdbobject Queryobject=NewBasicdbobject (). Append (Queryoperators.and,Newbasicdbobject[]{NewBasicdbobject ("Indusid", Rankparam.getindusid ()),NewBasicdbobject ("AreaCode", Startpattern),NewBasicdbobject ("AreaCode", Endpattern),NewBasicdbobject ("AreaCode",NewBasicdbobject ("$not", Notpattern))                                }); Finditerable=Collection.find (Queryobject); } mongocursor<Document> Mongocursor =Finditerable.iterator ();  while(Mongocursor.hasnext ()) {Document Document=Mongocursor.next (); Datalist.add (NewData (document.getstring ("name"), Document.getdouble ("value"), Document.getstring ("date"), Document.getstring (" AreaCode "), Document.getinteger (" Indusid "))); }        }Else{                    }        returndataList; }

Manipulating MongoDB in Java

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.