MongoDB 连接
package com.cdel.portal.core.mongo;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Properties;
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;
public class MongoDBManager {
/**
* 连接池
*/
private static Mongo mg = null;
/**
* you can think it is similar to connection
*/
private static DB db;
// private static final String dbAddress="192.168.190.49";
// private static final int dbPort=27017;
// private static final String dbName="lectureDB";
static{
try {
InputStream is = MongoDBManager.class.getClassLoader().getResourceAsStream("mongDB.properties");
Properties p = new Properties();
p.load(is);
Integer dbPort = Integer.parseInt((String)p.get( "dbPort") );
mg = new Mongo( (String)p.get( "dbAddress") ,dbPort );
db = mg.getDB((String) p.get("dbName") );
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public MongoDBManager() throws UnknownHostException, MongoException{
// users = db.getCollection("users");
}
/**
* 根据 集合的名字 得到集合对象
* @param collectionName
* @return
*/
public DBCollection getCollection(final String collectionName ){
return db.getCollection( collectionName);
}
/**
*
* @param dbCollection
* @param dbObject 添加数据
*/
public void AdddOneData(DBCollection dbCollection, DBObject dbObject){
dbCollection.insert( dbObject );
}
/**
*
* @param collection
* @param dbCondition 删除数据的条件
*/
public void remove(DBCollection collection ,DBObject dbCondition ){
collection.remove( dbCondition );
}
/**
* 更新数据
* @param collection
* @param updateCondition 根据这个条件 所要 更新的的数据
* @param updateResult 数据 更新后的结果
*/
public void update(DBCollection collection,DBObject updateCondition ,DBObject updateResult ){
collection.update( updateCondition , updateResult ,false,false );
}
/**
* 根据条件查询 相应的数据
* @param collection
* @return
*/
public List<DBObject> queryByCondition(DBCollection collection,DBObject condition ){
return collection.find(condition).toArray();
}
/**
* 取得查询结果集
* @param collection
* @return
*/
private static DBCursor getResult(DBCollection collection){
return collection.find();
}&nb