MongoDB Java Common Method recently wrote a public class for some common operations of MongoDB, hoping to help beginners.
package model.dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.bson.types.ObjectId;
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;
/**
* MongoDB Manager
* @author Ken Chau
*
*/
public class MongoDBManager {
private static Logger log = Logger.getLogger(MongoDBManager.class);
private static Mongo mg = null;
private static DB db = null;
private final static MongoDBManager instance = new MongoDBManager();
/**
* instantiation
* @return
* @throws Exception
*/
public static MongoDBManager getInstance() throws Exception {
return instance;
}
static {
try {
mg = new Mongo(HOST, PORT);
db=mg.getDB(DB);
} catch (Exception e) {
log.error(“Can’t connect MongoDB!”);
e.printStackTrace();
}
}
/**
* Get collection (table)
* @param collection
*/
public static DBCollection getCollection(String collection) {
Return db.getCollection(collection);
}
/**
* ———————————-Dividing line————- ————————-
*/
/**
* Insert
* @param collection
* @param map
*/
public void insert(String collection, Map map) {
try {
DBObject dbObject = map2Obj(map);
GetCollection(collection).insert(dbObject);
} catch (MongoException e) {
log.error(“MongoException:” + e.getMessage());
}
}
/**
* Batch Insert
* @param collection
* @param list
*/
public void insertBatch(String collection ,List<Map> list) {
If (list == nulsp; return getCollection(collection).getCount(map2Obj(map));
}
/**
** Calculate the total number of collections
* @param collection
* @param map
*/
public long getCount(String collection) {
Return getCollection(collection).find().count();
}
/**
* Update
* @param collection
* @param setFields
* @param whereFields
*/
public void update(String collection,Map setFields,
Map whereFields) {
DBObject obj1 = map2Obj(setFields);
DBObject obj2 = map2Obj(whereFields);
getCollection(collection).updateMulti(obj1, obj2);
}
/**
** Find objects (according to primary key_id)
* @param collection
* @param _id
*/
public DBObject findById(String collection,String _id) {
DBObject obj = new BasicDBObject();
Obj.put(“_id”, ObjectId.massageToObjectId(_id));
return getCollection(collection).findOne(obj);
}
/**
** Find all objects in the collection
* @param collection
*/
public List findAll(String collection) {
Return getCollection(collection).find().toArray();
}
/**
* Find (returns an object)
* @param map
* @param collection
*/
public DBObject findOne(String collection,Map map) {
DBCollection coll = getCollection(collection);
return coll.findOne(map2Obj(map));
}
/**
* Find (returns a List)
* @param
* @param map
* @param collection
* @throws Exception
*/
public List find(String collection,Map map) throws Exception {
DBCollection coll = getCollection(collection);
DBCursor c = coll.find(map2Obj(map));
if (c != null)
Return c.toArray();
else
return null;
}
Encapsulation class for Java to operate MongoDB database
This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/encapsulation-class-for-java-to-operate-mongodb-database/