|
, null);
}else{
collection=this.getDBCollection(collectionName, null, null, null);
}
result=collection.save( new BasicDBObject(map) );
} catch (Exception e) {
LogUtil.log.error(e);
e.printStackTrace();
} finally{
this.closeAll(mongo, db);
}
return result.getN();
}
/**
* 添加对象
* @param map
* @param collection
* @return
*/
public int addObjects(Map map,String collectionName){
WriteResult result=null;
try {
if(collectionName==null){
collection=this.getDBCollection(null, null, null, null);
}else{
collection=this.getDBCollection(collectionName, null, null, null);
}
result=collection.save( new BasicDBObject(map) );
} catch (Exception e) {
LogUtil.log.error(e);
e.printStackTrace();
} finally{
this.closeAll(mongo, db);
}
return result.getN();
}
/**
* 查询单个结果
* @param params
* @param collectionName
* @return
*/
public Map findOne(Map params,String collectionName){
Map map=new HashMap();
try {
if(collectionName==null){
collection=this.getDBCollection(null, null, null, null);
}else{
collection=this.getDBCollection(collectionName, null, null, null);
}
DBObject object;
if(params!=null){
object=collection.findOne( new BasicDBObject(params) );
}else{
object=collection.findOne();
}
Set keys=object.keySet();
for(String key:keys){
map.put(key, object.get(key));
}
}catch (MongoException e) {
LogUtil.log.error(e);
e.printStackTrace();
}finally{
this.closeAll(mongo, db);
}
return map;
}
public Object findOneToObject(Map params,String collectionName){
DBObject object = null;
try {
if(collectionName==null){
collection=this.getDBCollection(null, null, null, null);
}else{
collection=this.getDBCollection(collectionName, null, null, null);
}
if(params!=null){
object=collection.findOne( new BasicDBObject(params) );
}else{
object=collection.findOne();
}
}catch (MongoException e) {
LogUtil.log.error(e);
e.printStackTrace();
}finally{
this.closeAll(mongo, db);
}
return object;
}
public List |