Java code for operating mongodb, including basic addition, deletion, modification, and query operations to obtain the Database Connection Tool packagecom. liuc. db; importjava.net. unknownHostException; importcom. mongodb. DB; importcom. mongodb. DBCollection; importcom. mongodb. mongo; ***** @ briefMongoDBUtil. java operations
Java code for operating mongodb, including basic addition, deletion, modification, and query operations to obtain the Database Connection Tool package com. liuc. db; import java.net. unknownHostException; import com. mongodb. DB; import com. mongodb. DBCollection; import com. mongodb. mongo;/***** @ brief MongoDBUtil. java operations
Java code for mongodb operations, including basic addition, deletion, modification, and query operations
Obtain database connection tools
Package com. liuc. db; import java.net. unknownHostException; import com. mongodb. DB; import com. mongodb. DBCollection; import com. mongodb. mongo;/***** @ brief MongoDBUtil. java mongodb tool class * @ attention considerations * @ author liuchao * @ date 2013-12-30 * @ note begin modify by modifier modify time modify Content abstract description */public class MongoDBUtil {/* * ** \ brief no authentication required to obtain the database connection * @ return * @ attention note * @ author liuchao * @ date 2013-12-30 * @ Note begin modify by modifier modification time Description of modified content */public static DBCollection getDBConnectionWithoutAuth (String colName) {try {Mongo mongo = new Mongo ("localhost", 27017 ); DB db = mongo. getDB ("liuchao"); return db. getCollection (colName);} catch (UnknownHostException e) {e. printStackTrace (); return null ;}/ *** obtain the database connection to be authenticated start logon Password Authentication: log on to the database and add the user use db. addUser ('user1', 'pwd1 '); restart the server to enable the authentication service mongod -- auth -- dbpath = D: \ mongodb \ db next login requires user name and password authentication to start the client: use admin; // for login verification. If it fails, there is no operation permission. Db. auth ('user1', 'pwd1 '); */public static DBCollection getDBConnectionWithAuth (String colName) {try {Mongo mongo = new Mongo ("localhost", 27017 ); DB db = mongo. getDB ("liuchao"); char [] pwd_char = "liuchao ". toCharArray (); boolean auth = db. authenticate ("liuchao", pwd_char); // you can perform valid operations if (! Auth) {throw new RuntimeException ();} return db. getCollection (colName);} catch (UnknownHostException e) {e. printStackTrace (); return null ;}} public static void main (String [] args) {System. out. println (getDBConnectionWithAuth ("users"); System. out. println (getDBConnectionWithoutAuth ("users "));}}Basic addition, deletion, modification, and query operations:
Package com. liuc. db; import java. util. arrayList; import java. util. list; import com. mongodb. basicDBObject; import com. mongodb. DBCollection; import com. mongodb. DBCursor; import com. mongodb. DBObject;/***** @ brief MognoDBCURDUtil. java add, delete, modify, and query tool class * @ attention note * @ author liuchao * @ date 2013-12-30 * @ note begin modify by modifier modify time abstract description */public class MognoDBCURDUtil {/ * *** \ brief query all data ** @ attention Usage note * @ author liuchao * @ date 2013-12-30 * @ note begin modify by modifier modify time abstract description */private static void queryAll (String collectionName) {DBCollection users = MongoDBUtil. getDBConnectionWithAuth (collectionName); // db cursor DBCursor cur = users. find (); System. out. println ("Total number of data entries:" + users. count (); while (cur. hasNext () {System. out. println (cur. next ());}} /***** \ brief add data ** @ param collectionName * @ attention note * @ author liuchao * @ date 2013-12-30 * @ note begin modify by modifier modification time the modification summary shows the difference between save and insert. * The save function is based on the parameter conditions, the insert or update function is called. * If the data object to be inserted exists, the insert function reports an error while the save function changes the original object. * If the objects to be inserted do not exist, they perform the same insert operation. here we can use a few words to summarize the differences between the two, that is, the so-called "Yes, but no ". */public static void add (String collectionName, List
List) {DBCollection users = MongoDBUtil. getDBConnectionWithAuth (collectionName); users. insert (list ). getN ();} /***** \ brief update * @ param collectionName * @ param source * @ param target * @ attention note * @ author liuchao * @ date 2013-12-30 * @ note begin modify by modifier modify time modify Content abstract description */public static void update (String collectionName, DBObject source, DBObject target) {DBCollection users = MongoDBUtil. getDBConnectionWithAuth (collectionName);/*** true, // if the database does not exist, add false // multiple modifications */users. update (source, target, true, false );} /***** \ brief Delete * @ param collectionName * @ param obj * @ attention note * @ author liuchao * @ date 2013-12-30 * @ note begin modify by Modifier description of modified time Description */public static void delete (String collectionName, DBObject delObj) {DBCollection users = MongoDBUtil. getDBConnectionWithAuth (collectionName); users. remove (delObj); // print ("remove age> = 24:" + users. remove (new BasicDBObject ("age", new BasicDBObject ("$ gte", 24 ))). getN ());} /***** \ brief conditional query * @ param collectionName * @ param delObj * @ note for using the attention Method * @ author liuchao * @ date 2013-12-30 * @ note begin modify modified by modification time abstract description */public static void queryWithCondition (String collectionName, DBObject condition) {DBCollection users = MongoDBUtil. getDBConnectionWithAuth (collectionName); // users. find (new BasicDBObject ("_ id", new ObjectId ("4de73f7acd812d61b4626a77 "))). toArray () users. find (condition );}}
Code download: http://download.csdn.net/detail/shanhuhau/6788957