The recent focus on big data will naturally focus on the NoSQL database, which is of course mongodb.
NoSQL databases are mostly K,V databases, and this is not a new noun, berkerly db has been around for many years and now belongs to Oracle.
The specific K,V database is better than the traditional database optimization principle, but also need in-depth understanding and learning. At the same time the two should be the existence of space.
MongoDB is very simple to use, simply wrap the class as follows:
1 PackageDBHelper;2 3 Importcom.mongodb.*;4 ImportCom.mongodb.util.JSON;5 6 Importjava.net.UnknownHostException;7 Importjava.util.List;8 9 /**Ten * Created by Weijiabin on 2014/10/29. One */ A Public classMongoDB { - - PublicMongoDB ()throwsunknownhostexception { the_mongo =NewMongo (); - } - PrivateMongo _mongo; - PublicDB getdb (String strdbname) { + return_mongo.getdb (strDbName); - } + A Publicdbcollection getdbcollection (String strstrdbname, String strtablename) { at returnGetdb (strstrdbname). GetCollection (strtablename); - } - - Public voidAdd (String strdbname,string strtablename, DBObject obj) { - getdbcollection (strdbname,strtablename). Save (obj); - } in - Public voidAddlist (String strdbname,string strtablename, list<dbobject>objlist) { to getdbcollection (strdbname,strtablename). Insert (objlist); + } - the Public intRemove (String strdbname,string strtablename,dbobject obj) { * returngetdbcollection (strdbname,strtablename). Remove (obj). GETN (); $ }Panax Notoginseng - Public intModify (String strdbname,string strtablename,dbobject objq,dbobject Objn,BooleanBupdateall) { the returnGetdbcollection (strdbname,strtablename). Update (OBJQ,OBJN,true, Bupdateall). GETN (); + } A the Publicdbobject Query (String strdbname,string strtablename,dbobject objq) { + returngetdbcollection (strdbname,strtablename). FindOne (OBJQ); - } $ $ - -}
The test code is as follows:
PackageDBHelper;ImportCom.mongodb.BasicDBObject;ImportCom.mongodb.DBObject;Importorg.junit.Test;Import Staticorg.junit.assert.*; Public classmongodbtest {@Test Public voidTestgetdb ()throwsException {MongoDB db=NewMongoDB (); Basicdbobject obj=NewBasicdbobject (); Obj.put ("Name", "Weijiabin"); Obj.put ("Sex", "man"); Obj.put ("Age", 32); Db.add ("DB1", "Users", obj); Basicdbobject OBJQ=NewBasicdbobject ("Age", 32); DBObject OBJR= Db.query ("DB1", "Users", OBJQ); Assertequals ("Weijiabin", Objr.get ("name")); }}
It's quite simple and easy to see. The direct manipulation of JSON data structures has the advantage of XML and is more suitable for network transmission than XML.
MongoDB First Experience