Use of Mongodb underlying java driver framework tools

Source: Internet
Author: User
Tags findone
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 the document efficiently. The random key can also be indexed, which is more efficient. Reasonable choice and design. Importjava.net. Unknown

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 the document efficiently. The random key can also be indexed, which is more efficient. Reasonable choice and design. Import java.net. Unknown

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 **/public class extends util {private final static ThreadLocal
 
  
Mongos = new ThreadLocal
  
   
(); 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 (UnknownHostException e) {e. printStackTrace ();} catch (except exception e) {e. printStackTrace () ;}} r Eturn 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 * List of inserts */public void insertBatch (String collection, list
 
  
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
 
  
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);}/*** query all objects in the set ** @ param collection */public static List
  
   
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
   
    
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
    
     
FindLess (String collection, DBObject q, DBObject fileds, int pageNo, int perPageCount) {return getCollection (collection ). find (q, fileds ). skip (pageNo-1) * perPageCount ). limit (perPageCount ). toArray ();}
    
   
  
 


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.