MongoDB First Experience

Source: Internet
Author: User

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

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.