MongoDB Exploration Series Four: MongoDB and Java dances

Source: Internet
Author: User
Tags mongoclient

Depending on the version, the API may be different, and the 3.0 version is used for this study.

1. The JDBC driver version of MongoDB used is: Mongo-java-driver-3.0.0.jar

2. This section simply introduces JDBC operations and does not consider efficiency issues for the time being.

3, the package of tool class code is as follows:

public class Mongodbproxy {private static mongodbproxy proxy=null;//Single Instance private static Mongodatabase db=null;// Database Connection object private static String [] paramarray=new string[5];//database connection parameter Private Mongodbproxy () {}static{paramarray[0]= " Username ";p aramarray[1]=" password ";p aramarray[2]=" host ";p aramarray[3]=" port ";p aramarray[4]=" DatabaseName ";} /** * Get Mongodbproxy * with system default configuration */public static Mongodbproxy Getmongodbproxy () {if (proxy==null) {proxy=new mongodbproxy () ; String SURI = String.Format ("mongodb://%s:%[email protected]%s:%d/%s", Paramarray[0],paramarray[1],paramarray [2],integer.parseint (Paramarray[3]), paramarray[4]); Mongoclienturi uri = new Mongoclienturi (SURI); Mongoclient mongoclient = new Mongoclient (URI); db= mongoclient.getdatabase (paramarray[4]);} return proxy;} /** * Bulk Query data * @param table collection name * @param page Paging parameter * @param filter filter condition * @param sort sort Condition */public page finddocli St (String table,page page,bson filter,bson sort) {mongocollection<document> coll=db.getcollection (table);Long Count=coll.count (filter);//Gets the total amount of data based on the filtering criteria page P=pageutil.createpage (Page,integer.parseint (string.valueof ( count));p. Setfromurl ((Page.getfromurl () ==null)? ":p Age.getfromurl ());p. Setparamstring ((page.getparamstring () = =null)? "":p age.getparamstring ()); Finditerable<document> resultiterable=coll.find ();//Perform conditional filtering resultiterable=resultiterable.sort (sort). filter (filter). Skip (P.getbeginindex ()). BatchSize (P.geteverypage ()); Mongocursor<document> Cousor=resultiterable.iterator (); List<document> datalist=new arraylist<document> (); while (Cousor.hasnext ()) {Datalist.add (Cousor.next () );} P.setdatalist (dataList); return pageutil.buildpagestring (P);} /** * Get a single document * @param table collection Name * @param filter Filter condition * @param sort Sort Criteria */public document Findonedoc (String table,bs On Filter,bson sort) {mongocollection<document> coll=db.getcollection (table); Finditerable<document> Resultiterable=coll.find (); if (sort!=null) {resultiterable.sort (sort);} if (filter!=null) {Resultiterable.filter(filter);} return Resultiterable.first ();} /** * Add document * @param table collection name * @prama Doc document content */public void Adddocument (String table,document doc) {MONGOCOLLECTION&L t;document> coll=getcollection (table); Coll.insertone (DOC);} /** * Bulk Add document * @param table collection name * @prama docList document collection */public void Adddocumentlist (String table,list<document& Gt docList) {mongocollection<document> coll=getcollection (table); Coll.insertmany (docList);} /** * Update document * @param table collection name * @param query condition * @param up update data */public updateresult updatedocument (String tab Le,bson Query,bson up) {mongocollection<document> coll=getcollection (table); return Coll.updateone (Query,up);}  /** * Replace document * @param table collection name * @param query condition * @param up Replace file object */public updateresult replacedocument (String Table,bson query,document up) {mongocollection<document> coll=getcollection (table); return Coll.replaceone ( query, up);} /** * Delete Document * @param table collection name * @param delete delete condition */public DeleteresulT deletedocument (String table,bson Delete) {mongocollection<document> coll=getcollection (table); return Coll.deleteone (delete);} /** * Get Collection Object * @param table collection name */private mongocollection<document> getcollection (String table) {return Db.getcol Lection (table);}}

4. Call Demo

Mongodbproxy Proxy=mongodbproxy.getmongodbproxy (); System.out.println (Proxy.findonedoc ("Users", Null,null). Get ("_id"));D ocument doc=new Document ();d oc.put ("User", " John Doe ");p roxy.adddocument (" Users ", Doc); Bson bson=new basicdbobject ("User", "Zhang San");p roxy.deletedocument ("Users", Bson);

Follow-up further study, first use demo on the start Wow, haha.

Copyright NOTICE: Reprint Please specify the address of the blog, respect the author's labor results. Welcome to pay attention to http://blog.csdn.net/zgs_shmily, grow together.

MongoDB Exploration Series Four: MongoDB and Java dances

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.