Package Com.x.mongodb; Import java.net.UnknownHostException; Import Java.util.HashMap; Import java.util.List; Import Java.util.Map; Import Org.bson.types.ObjectId; Import Com.mongodb.BasicDBObject; Import Com.mongodb.DB; Import com.mongodb.DBCollection; Import Com.mongodb.DBObject; Import Com.mongodb.Mongo; Import com.mongodb.MongoException; Public final class Mongodbutil {private static final String HOST = "127.0.0.1"; private static final String DbName = "Test"; private static Mongo Mongo; private static DB db; Static {try {MONGO = new MONGO (HOST); db = Mongo.getdb (dbName); Db.authenticate (username, passwd)} catch (Unknownhostexception e) {e.printstacktrace (); } catch (Mongoexception e) {e.printstacktrace (); }} private Mongodbutil () {}/** * Add action * <br>------------------------------<br> * @param map * @param collectionname */public static void Add (Map<string, Object> ; Map, String collectionname) {dbobject dbobject = new Basicdbobject (map); GetCollection (CollectionName). Insert (DBObject); }/** * Add operation * <br>------------------------------<br> * @param list * @param co Llectionname */public static void Add (List<map<string, object>> List, String collectionname) { For (map<string, object> map:list) {Add (Map, CollectionName); }}/** * Delete operation * <br>------------------------------<br> * @param map * @pa Ram CollectionName */public static void Delete (Map<string, object> Map, String collectionname) { DBObject dbobject = new Basicdbobject (map); GetCollection (CollectionName). Remove (DBObject); }/** * Delete operation, based on primary key * <br>------------------------------<br> * @param ID * @param collectionname */ public static void Delete (string id, string collectionname) {map<string, object> Map = new Hashmap<s Tring, object> (); Map.put ("_id", new ObjectId (ID)); Delete (map, collectionname); }/** * Delete all * <br>------------------------------<br> * @param collectionname * /public static void DeleteAll (String collectionname) {getcollection (CollectionName). Drop (); }/** * Modify action </br> * will replace the existing document with a new document, the document key structure will change </br> * such as the original document {"_id": "123", "Name": "Zhan Gsan "," Age ": 12} When you modify the age * value to {" Age "according to _id: 12} The newly created document name value will not be changed, the structure changes * <br>------------------------------&L t;br> * @param wheremap * @param valueMap * @param collectionname */public static void Update (map<string, object> Wheremap, Map<string, object> ValueMap, String collectionname) {executeupdate (CollectionName, Wheremap, ValueMap, n EW Updatecallback () {public dbobject docallback (DBObject valuedbobject) {return valuedbobje Ct } }); }/** * Modify operation, use $set modifier </br> * To specify a key value, if the key does not exist, it is automatically created, will update the original document, will not generate new, the structure will not change * <br> ;------------------------------<br> * @param wheremap * @param valueMap * @param collecti Onname */public static void Updateset (Map<string, object> wheremap, map<string, object> ValueMap, S Tring CollectionName) {executeupdate (CollectionName, Wheremap, ValueMap, New Updatecallback () {Publ IC dbobject docallback (dbobject valuedbobject) {return new Basicdbobject ("$set", valuedbobject); } }); }/** * Modify operation, use $inc modifier </br> * Modifier key value must be numeric </br> * If the key exists to increase or decrease the value of the key, if no creation key exists * <br>------------------------------<br> * @param wheremap * @param valueMap * @param collectionname */public static void Updateinc (Map<string, object> wh Eremap, map<string, integer> ValueMap, String collectionname) {executeupdate (CollectionName, Wheremap, Val Uemap, New Updatecallback () {public dbobject docallback (DBObject valuedbobject) {return new Basicdbobject ("$inc", valuedbobject); } }); }/** * Modify * <br>------------------------------<br> * @param collectionname * @param wheremap * @param valueMap * @param updatecallback */private static void Executeupdate (String CollectionName, map Wheremap, map ValueMap, Updatecallback updatecallback) {dbobject wheredbobject = new basicdb Object (WHEREMAP); DBObject valuedbobject = new Basicdbobject (valuEMAP); Valuedbobject = Updatecallback.docallback (Valuedbobject); GetCollection (CollectionName). Update (Wheredbobject, valuedbobject); } interface Updatecallback {dbobject docallback (dbobject valuedbobject); }/** * Get collection (table) * <br>------------------------------<br> * @param collectionname * @return */public static dbcollection GetCollection (String collectionname) {return db.getcollectio N (collectionname); } }
Mongodb-java API additions and deletions operation