The example is based on the current latest MONGO driver, version Mongo-2.10.1.jar, and unit tests written in JUnit.
Testcase.java Java code package com.wujintao.mongo; import java.net.unknownhostexception; import java.util.arraylist; import java.util.list; import java.util.set; import java.util.regex.pattern; import org.junit.test; import com.mongodb.aggregationoutput ; import com.mongodb.basicdblist; import com.mongodb.basicdbobject; import com.mongodb.basicdbobjectbuilder; import com.mongodb.db; Import com.mongodb.DBCollection; import com.mongodb.dbcursor; import com.mongodb.dbobject; import com.mongodb.mapreducecommand; import com.mongodb.mapreduceoutput; import com.mongodb.mongo; import com.mongodb.querybuilder; Import com.monGodb. writeconcern; public class testcase { //dbcursor cursor = coll.find (condition). AddOption (Bytes.QUERYOPTION_ Notimeout)//Set cursor do not time out @Test /** * Get all DB instances */ public void testgetdbs () { list<string> dbnames = mongoutil.getmong (). GetDatabaseNames (); for (string dbname : dbnames) { system.out.println ("dbname : " + dbname); } } @Test /** * Delete databases */ public void dropdatabase () { Mongoutil.getmong (). Dropdatabase ("my_new_db"); } @Test /** * Query all table names */ public void Getallcollections () { Set<String> Colls = mongoutil.getdb (). Getcollectionnames (); for (string s : colls) { &nbSp; system.out.println (s); } } @Test public void dropcollection () { mongoutil.getcoll ("Jellonwu"). Drop (); } /** * Add a record */ @Test public void adddata () { DBCollection coll = Mongoutil.getcoll ("Wujintao"); BasicDBObject Doc = new basicdbobject (); doc.put (" Name ", " MOngodb "); doc.put (" type ", " database "); doc.put ("Count", 1); basicdbobject info = new basicdbobject (); info.put ("x", 203); info.put ("Y", 102); doc.put ("info", info); coll.insert (DOC); // set Write concern to be prompted when the operation fails coll.setwriteconcern (Writeconcern.safe);