Depending on the version number, the API may also be different. This study uses the 3.0 version number.
1. The JDBC driver version number used for MongoDB is: Mongo-java-driver-3.0.0.jar
2. This section simply introduces JDBC operations and does not consider efficiency issues on a temporary basis.
3, the package of tool class code such as the following:
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 * Adopted 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 Page Count * @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);A 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 ();//Run Condition filter 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);
Perhaps further study, first use the demo to get started Wow. Ha ha.
MongoDB Exploration Series Four: MongoDB and Java dances