MongoDB操作:update()
2016-03-11 00:00
239 查看
@Override
public boolean update(String dbName, String collectionName,
DBObject oldValue, DBObject newValue) {
DB db = null;
DBCollection dbCollection = null;
WriteResult result = null;
String resultString = null;
if(oldValue.equals(newValue)){
return true;
}else{
try {
db = mongoClient.getDB(dbName); //获取数据库实例
dbCollection = db.getCollection(collectionName); //获取数据库中指定的collection集合
result = dbCollection.update(oldValue, newValue);
resultString = result.getError();
return (resultString!=null) ? false : true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
if(null != db){
db.requestDone(); //关闭db
db = null;
}
}
}
return false;
}
public boolean update(String dbName, String collectionName,
DBObject oldValue, DBObject newValue) {
DB db = null;
DBCollection dbCollection = null;
WriteResult result = null;
String resultString = null;
if(oldValue.equals(newValue)){
return true;
}else{
try {
db = mongoClient.getDB(dbName); //获取数据库实例
dbCollection = db.getCollection(collectionName); //获取数据库中指定的collection集合
result = dbCollection.update(oldValue, newValue);
resultString = result.getError();
return (resultString!=null) ? false : true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
if(null != db){
db.requestDone(); //关闭db
db = null;
}
}
}
return false;
}
相关文章推荐
- MongoDB下载及安装
- MongoDB存储数据
- MongoDB接口类函数
- MongoDB基本操作(增删改查)
- mongodb注册service启动失败(异常1053)
- linq查询语句转mongodb
- Spring-Data-MongoDB保存对象的时候,如何不保存_class字段
- Mongodb的安装--简单快速
- MongoDB学习—(5)修改器$inc,$unset,$push,$pushAll,$allToSet,$pop,$pull,$pullAll
- MongoDB简单查询与复杂查询性能对比
- Mongodb第八天 JavaAPi
- MongoDB学习笔记(索引)
- MongoDB学习笔记(查询)
- mongodb基础系列——java操作mongodb实现CURD
- MongoDB 性能优化五个简单步骤
- MongoDB shell操作
- Mongodb在windows下的安装和启动
- 通过Nginx+mongodb-gridfs+squid实现简单的图片存储及图片缓存示例
- mongoDB--GridFS简介
- 从百万级别数据的分析角度,Mysql,Mongodb,Hbase如何选择?