use of com.duangframework.core.exceptions.MongodbException in project duangframework by tcrct.
the class MongoBaseDao method update.
/**
* 根据条件更新记录
* @param mongoQuery 查询条件
* @param mongoUpdate 更新内容
* @return 成功更新的记录数
* @throws Exception
*/
@Override
public long update(MongoQuery mongoQuery, MongoUpdate mongoUpdate) throws Exception {
Bson queryBson = mongoQuery.getQueryBson();
Bson updateBson = mongoUpdate.getUpdateBson();
if (ToolsKit.isEmpty(queryBson) || ToolsKit.isEmpty(updateBson)) {
throw new MongodbException("Mongodb Update is Fail: queryBson or updateBson is null");
}
// 3.5以上的版体写法,为了支持3.5以下的版本,故注释掉
// BsonDocument bsonDocument = document.toBsonDocument(cls, collection.getCodecRegistry());
// 查询记录不存在时,不新增记录
UpdateOptions options = new UpdateOptions();
options.upsert(false);
UpdateResult updateResult = collection.updateOne(queryBson, updateBson, options);
return updateResult.isModifiedCountAvailable() ? updateResult.getModifiedCount() : 0L;
}
use of com.duangframework.core.exceptions.MongodbException in project duangframework by tcrct.
the class MongoBaseDao method findOne.
/**
* 根据条件查询记录
* @param mongoQuery 查询条件对象
* @return 泛型对象
* @throws Exception
*/
@Override
public T findOne(MongoQuery mongoQuery) throws Exception {
if (ToolsKit.isEmpty(mongoQuery)) {
throw new MongodbException("Mongodb findOne is Fail: mongoQuery is null");
}
Bson queryDoc = mongoQuery.getQueryBson();
Document document = collection.find(queryDoc).first();
if (ToolsKit.isEmpty(document)) {
return null;
}
return MongoUtils.toEntity(document, cls);
}
use of com.duangframework.core.exceptions.MongodbException in project duangframework by tcrct.
the class MongoBaseDao method update.
private boolean update(String id, Document document) throws Exception {
if (!ObjectId.isValid(id)) {
throw new MongodbException("id is not ObjectId!");
}
Document query = new Document(IdEntity.ID_FIELD, new ObjectId(id));
// 查询记录不存在时,不新增记录
UpdateOptions options = new UpdateOptions();
options.upsert(false);
document.remove(IdEntity.ENTITY_ID_FIELD);
BasicDBObject updateDbo = new BasicDBObject(Operator.SET, document);
return collection.updateOne(query, updateDbo, options).isModifiedCountAvailable();
}
Aggregations