A simple MongoDB operation class

Source: Internet
Author: User
Import java.net. unknownhostexception; import Java. util. map; import Java. util. concurrent. concurrenthashmap; import Org. slf4j. logger; import Org. slf4j. loggerfactory; import COM. mongoDB. basicdbobject; import COM. mongoDB. DB; import COM. mongoDB. dbcollection; import COM. mongoDB. dbcursor; import COM. mongoDB. dbobject; import COM. mongoDB. mongo; import COM. mongoDB. writeresult; import COM. mongoDB. util. JSON;/*** MongoDB operations Class * @ author DYK **/public class mongodbmanagerimpl implements mongodbmanager {protected static logger = loggerfactory. getlogger ("03046"); Private Static final string dbname = "Clementine"; private Mongo = NULL; private dB dbconnection = NULL; private string implements serveraddr; private int implements SERVERPORT; private Static Map <string, dbcollection> dbcollectionmap = new concurrenthashmap <string, Dbcollection> (); Public void setincluserveraddr (string incluserveraddr) {This. protected serveraddr = protected serveraddr;} public void setprotected SERVERPORT (INT protected SERVERPORT) {This. export SERVERPORT = export SERVERPORT;} public void Init () {If (this. mongo = NULL) {try {This. mongo = new Mongo (this. mongoserveraddr, this. mongoserverport); If (null! = This. mongo) {This. dbconnection = This. mongo. getdb (dbname) ;}} catch (unknownhostexception e) {logger. error ("failed to connect to MongoDB, server address:" + this. mongoserveraddr + ", Port:" + this. mongoserverport); throw new runtimeexception (e) ;}}/ *** get an table * @ Param collectionname * @ return */private dbcollection getdbcollection (string collectionname) {dbcollection collection = NULL; If (dbcollectionmap. containskey (Collectionname) {collection = dbcollectionmap. Get (collectionname);} else {collection = This. dbconnection. getcollection (collectionname); If (null! = Collection) {dbcollectionmap. put (collectionname, Collection) ;}} return collection ;} /*** check if Doc exsit * @ Param collectionname table name * @ Param query target document */@ overridepublic Boolean isincluentexsit (string collectionname, dbobject query) {boolean result = false; dbcursor = NULL; dbcollection collection = This. getdbcollection (collectionname); If (null! = Collection) {dbcursor = collection. Find (query); If (null! = Dbcursor & dbcursor. hasnext () {result = true;} return result ;} /*** query an record * @ Param collectionname table name * @ Param query target document * @ return */@ overridepublic dbobject selectdocument (string collectionname, dbobject query) {dbobject result = NULL; dbcursor = NULL; dbcollection collection = This. getdbcollection (collectionname); If (null! = Collection) {dbcursor = collection. Find (query); If (null! = Dbcursor & dbcursor. hasnext () {result = dbcursor. next () ;}} return result ;} /*** insert an new record * @ Param collectionname table name * @ Param newdocument New Doc * @ Param query target document */@ overridepublic void insertdocument (string collectionname, dbobject newdocument) {dbcollection collection = This. getdbcollection (collectionname); If (null! = Collection) {If (! This. isdocumentexsit (collectionname, newdocument) {// insert only doc not exist collection. insert (newdocument );}}} /*** update an document * @ Param collectionname * @ Param query target document * @ Param updateddocument * @ return */@ overridepublic Boolean updatedocument (string collectionname, dbobject query, dbobject updateddocument) {boolean result = false; writeresult = NULL; dbcollectio N collection = This. getdbcollection (collectionname); If (null! = Collection) {writeresult = collection. Update (query, updateddocument); If (null! = Writeresult) {If (writeresult. getn ()> 0) {result = true ;}} return result ;} /*** delete an document * @ Param collectionname * @ Param document target document * @ return */@ overridepublic Boolean deletedocument (string collectionname, dbobject query) {boolean result = false; writeresult = NULL; dbcollection collection = This. getdbcollection (collectionname); If (null! = Collection) {writeresult = collection. Remove (query); If (null! = Writeresult) {If (writeresult. getn ()> 0) {result = true ;}} return result;} public static void main (string [] ARGs) throws unknownhostexception {mongodbmanagerimpl M = new mongodbmanagerimpl (); m. setmongoserveraddr ("10.235.164.180"); M. setmediaserverport (27017); M. init (); string collectionname = "mytest"; // insertstring JSON = "{'num': 1}"; dbobject doc1 = (dbobject) JSON. parse (JSON); M. insertdocument (collectionname, doc1); // selectdbobject doc2 = NULL; dbobject query = new basicdbobject (); query. put ("num", 1); doc2 = m. selectdocument (collectionname, query); system. out. println (doc2); // updatedbobject updateddocument = new basicdbobject (); updateddocument. put ("$ set", new basicdbobject (). append ("num", 100); boolean result = m. updatedocument (collectionname, query, updateddocument); system. out. println (result); query. put ("num", 100); // removeresult = m. deletedocument (collectionname, query); system. out. println (result );}}

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.