Use of Mongodb underlying java driver framework tools and mongodb tools
To use MongoDB, you must properly design the document structure to meet specific requirements. For example, if a document is randomly selected and skip is used to skip a random document, no random key is added to the document,
Then, a random number is used to query documents efficiently. random keys can also be used to add indexes, which is more efficient. Reasonable choice and design.
Import java.net. unknownHostException; import java. util. date; import java. util. list; import com. mongodb. basicDBList; 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. except exception; import com. nerd. mongo. config. configFactory;/*** MONGOS * @ author chenlongquan **/publ Ic class required util {private final static ThreadLocal <Mongo> mongos = new ThreadLocal <Mongo> (); public static DB getdb () {return getMongos (). getDB (ConfigFactory. getmediaconfig (). getDb ();} public static Mongo getMongos () {Mongo mongo = mongos. get (); if (mongo = null) {try {mongo = new Mongo (ConfigFactory. getmediaconfig (). getIp (), ConfigFactory. getmediaconfig (). getPort (); mongos. set (mongo);} catch (Unkn OwnHostException e) {e. printStackTrace ();} catch (except exception e) {e. printStackTrace () ;}return mongo;} public static void close () {Mongo mongo = mongos. get (); if (mongo! = Null) {mongo. close (); mongos. remove () ;}}/*** get set (table) ** @ param collection */public static DBCollection getCollection (String collection) {return getdb (). getCollection (collection );}.................................. ....................
Next we can perform basic crud operations on mongodb based on the above.
For example:
/*** Insert ** @ param collection * @ param o insert **/public static void insert (String collection, DBObject o) {getCollection (collection ). insert (o);}/*** batch insert ** @ param collection * @ param list * inserted List */public void insertBatch (String collection, list <DBObject> list) {if (list = null | list. isEmpty () {return;} getCollection (collection ). insert (list );}
Test cases:
insert("user1",new BasicDBObject().append("name", "admin3").append("type", "2").append("score", 70) .append("level", 2).append("inputTime", new Date().getTime()));
Tool usage:
/*** Delete ** @ param collection * @ param q * query condition */public void delete (String collection, DBObject q) {getCollection (collection ). remove (q);}/*** batch Delete ** @ param collection * @ param list * Delete condition List */public void deleteBatch (String collection, list <DBObject> list) {if (list = null | list. isEmpty () {return;} for (int I = 0; I <list. size (); I ++) {getCollection (collection ). remove (list. get (I ));}} /*** update ** @ param collection * @ param q * query condition * @ param setFields * update object */public static void update (String collection, DBObject q, DBObject setFields) {getCollection (collection ). updateMulti (q, new BasicDBObject ("$ set", setFields ));} /*** search for all objects in the Set ** @ param collection */public static List <DBObject> findAll (String collection) {return getCollection (collection ). find (). toArray ();} /*** query all objects in the Set in sequence ** @ param collection * dataset * @ param orderBy * sorting */public static List <DBObject> findAll (String collection, DBObject orderBy) {return getCollection (collection ). find (). sort (orderBy ). toArray ();}/*** search (returns an object) ** @ param collection * @ param q * query condition */public static DBObject findOne (String collection, DBObject q) {return getCollection (collection ). findOne (q);}/*** search (returns an object) ** @ param collection * @ param q * query condition * @ param fileds * returned field */public static DBObject findOne (String collection, DBObject q, DBObject fileds) {return getCollection (collection ). findOne (q, fileds);}/*** query the collection object by page, return a specific field ** @ param collection * @ param q * query condition * @ param fileds * return field * @ pageNo page n * @ perPageCount number of records per page */public static List <DBObject> findLess (String collection, DBObject q, DBObject fileds, int pageNo, int perPageCount) {return getCollection (collection ). find (q, fileds ). skip (pageNo-1) * perPageCount ). limit (perPageCount ). toArray ();}