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