Search in sources :

Example 1 with MongodbException

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;
}
Also used : UpdateOptions(com.mongodb.client.model.UpdateOptions) UpdateResult(com.mongodb.client.result.UpdateResult) Bson(org.bson.conversions.Bson) MongodbException(com.duangframework.core.exceptions.MongodbException)

Example 2 with MongodbException

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);
}
Also used : Document(org.bson.Document) MongodbException(com.duangframework.core.exceptions.MongodbException) Bson(org.bson.conversions.Bson)

Example 3 with MongodbException

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();
}
Also used : ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) MongodbException(com.duangframework.core.exceptions.MongodbException)

Aggregations

MongodbException (com.duangframework.core.exceptions.MongodbException)3 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 Document (org.bson.Document)2 Bson (org.bson.conversions.Bson)2 UpdateResult (com.mongodb.client.result.UpdateResult)1 ObjectId (org.bson.types.ObjectId)1