The MongoDB underlying Java Driver Framework tool class uses

Source: Internet
Author: User
Tags bulk insert findone

Using MONGODB requires a reasonable design of the document structure to meet certain specific requirements. For example, randomly selecting a document, skipping a random document with Skip and not adding a random key to the document.

Then use a random number to query the document efficiently, the random key can also add indexes, more efficient. Reasonable choice, reasonable 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.mongoexception;import com.nerd.mongo.config.configfactory;/** * MONGOS * @author Chenlongquan * */public class Mongoutil {private final static T hreadlocal<mongo> MONGOs = new threadlocal<mongo> ();p ublic static DB Getdb () {return Getmongos (). GETDB ( Configfactory.getmongoconfig (). GETDB ());} public static Mongo Getmongos () {Mongo Mongo = Mongos.get (), if (Mongo = = null) {try {Mongo = new Mongo (CONFIGFACTORY.GETMO Ngoconfig (). GetIP (), Configfactory.getmongoconfig (). Getport ()); Mongos.set (MONGO);} catch (Unknownhostexception e) {e.printstacktrace ();} catch (Mongoexception e) {e.printstacktrace ()}} return MONGO;} public static void Close () {Mongo Mongo = Mongos.get (); if (mongo!=null) {Mongo.close (); Mongos.remove ();}} /** * Get collection (table) * * @param collection */public static Dbcollection GetCollection (String collection) {return Getdb (). Getcoll Ection (collection);} ......................................................
Below, we can perform basic CRUD operations on MongoDB based on this

For example:

/** * Insert *  * @param collection * @param o insert *     */public static void Insert (String collection, DBObject o) {getcoll Ection (collection). Insert (o);} /** * BULK INSERT *  * @param collection * @param list *            insert */public void Insertbatch (String collection, List<dbobje Ct> list) {if (list = = NULL | | list.isempty ()) {return;} GetCollection (collection). Insert (list);}

Test Case:

Insert ("user1", New Basicdbobject (). Append ("name", "Admin3"). Append ("type", "2"). Append ("Score"). Append ("Level" , 2). Append ("Inputtime", New Date (). GetTime ()));

Tool class use:

/** * Delete * * @param collection * @param q * Query condition */public void Delete (String collection, DBObject q) {Getcolle Ction (collection). Remove (q); /** * Bulk Delete * * @param collection * @param list * Delete Condition list */public void Deletebatch (String collection, list<d Bobject> 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 conditions * @param setfields * Update object */public static void upd Ate (String collection, DBObject Q, DBObject setfields) {GetCollection (collection). Updatemulti (Q,new basicdbobject ("$ Set ", Setfields));} /** * Find Collection all objects * * @param collection */public static list<dbobject> FindAll (String collection) {return getcollectio N (Collection). Find (). ToArray ();} /** * Find collection all objects sequentially * @param collection * DataSet * @param ORDER BY * sort */public static List<dbobjec T> FindAll (String collection, DbobjecT) {return GetCollection (collection). Find (). sort (by). ToArray ();} /** * FIND (return an object) * * @param collection * @param q * Query conditions */public static DBObject FindOne (String collection, D Bobject q) {return GetCollection (collection). FindOne (q);} /** * FIND (return an object) * * @param collection * @param q * Query condition * @param fileds * return field */public static DBO Bject FindOne (String collection, DBObject Q, DBObject fileds) {return GetCollection (collection). FindOne (q, fileds);} /** * Page Find collection object, return specific fields * * @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 pa Geno,int perpagecount) {return GetCollection (collection). Find (q, fileds). Skip ((pageNo-1) * perpagecount). Limit ( Perpagecount). ToArray ();}


The MongoDB underlying Java Driver Framework tool class uses

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.