Search in sources :

Example 1 with MongoQuery

use of com.duangframework.mongodb.common.MongoQuery in project duangframework by tcrct.

the class CurdService method delete.

/**
 * 删除记录
 */
public boolean delete(String id) throws ServiceException {
    if (!ToolsKit.isValidDuangId(id)) {
        throw new ServiceException("it is not ObjectId");
    }
    MongoQuery<T> mongoQuery = new MongoQuery<>();
    mongoQuery.eq(IdEntity.ID_FIELD, id);
    MongoUpdate<T> mongoUpdate = new MongoUpdate<>();
    mongoUpdate.set(IdEntity.STATUS_FIELD, IdEntity.STATUS_FIELD_DELETE);
    try {
        long count = getMongoDao().update(mongoQuery, mongoUpdate);
        if (count > 0 && (getCacheDao() != null)) {
            getCacheDao().delete(entityClass, id);
        }
        return true;
    } catch (Exception e) {
        throw new ServiceException("delete id[" + id + "] record is fail: " + e.getMessage(), e);
    }
}
Also used : ServiceException(com.duangframework.core.exceptions.ServiceException) MongoUpdate(com.duangframework.mongodb.common.MongoUpdate) MongoQuery(com.duangframework.mongodb.common.MongoQuery) ServiceException(com.duangframework.core.exceptions.ServiceException) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Example 2 with MongoQuery

use of com.duangframework.mongodb.common.MongoQuery in project duangframework by tcrct.

the class CurdService method findById.

/**
 * 查找记录
 */
public <T> T findById(String id) throws ServiceException {
    if (!ToolsKit.isValidDuangId(id)) {
        throw new ServiceException("it is not ObjectId");
    }
    T recordObj = null;
    try {
        if (getCacheDao() != null) {
            recordObj = (T) getCacheDao().findById(id, getEntityClass());
            if (ToolsKit.isNotEmpty(recordObj)) {
                return recordObj;
            }
        }
        MongoQuery<T> mongoQuery = new MongoQuery<>();
        mongoQuery.eq(IdEntity.ID_FIELD, id);
        recordObj = (T) getMongoDao().findOne(mongoQuery);
        if (ToolsKit.isNotEmpty(recordObj)) {
            if (getCacheDao() != null) {
                getCacheDao().save((IdEntity) recordObj);
            }
        }
    } catch (Exception e) {
        throw new ServiceException("findById id[" + id + "] record is fail: " + e.getMessage(), e);
    }
    return recordObj;
}
Also used : ServiceException(com.duangframework.core.exceptions.ServiceException) MongoQuery(com.duangframework.mongodb.common.MongoQuery) ServiceException(com.duangframework.core.exceptions.ServiceException) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Example 3 with MongoQuery

use of com.duangframework.mongodb.common.MongoQuery in project duangframework by tcrct.

the class MongoKit method clear.

private static void clear() {
    mongoQuery = new MongoQuery();
    mongoUpdate = new MongoUpdate();
}
Also used : MongoUpdate(com.duangframework.mongodb.common.MongoUpdate) MongoQuery(com.duangframework.mongodb.common.MongoQuery)

Example 4 with MongoQuery

use of com.duangframework.mongodb.common.MongoQuery in project duangframework by tcrct.

the class MongoBaseDao method unset.

/**
 * 根据指定的ObjectId,删除指定的字段属性
 * @param keys			要删除的字段属性
 * @return						返回受影响的记录数
 */
public int unset(String id, String... keys) {
    if (ToolsKit.isEmpty(id)) {
        throw new NullPointerException("id is null");
    }
    if (ToolsKit.isEmpty(keys)) {
        throw new NullPointerException("keys is null");
    }
    MongoQuery query = new MongoQuery();
    query.eq(IdEntity.ID_FIELD, id);
    DBObject dbo = new BasicDBObject();
    for (String key : keys) {
        dbo.put(key, 1);
    }
    DBObject update = new BasicDBObject(Operator.UNSET, dbo);
    WriteResult result = coll.updateMulti(query.getQueryObj(), update);
    return result.getN();
}
Also used : MongoQuery(com.duangframework.mongodb.common.MongoQuery)

Example 5 with MongoQuery

use of com.duangframework.mongodb.common.MongoQuery in project duangframework by tcrct.

the class MongoBaseDao method unset.

/**
 * 根据指定的ObjectId集合,批量删除指定的字段属性
 * @param keys			要删除的字段属性
 * @return						返回受影响的记录数
 */
public int unset(Set<String> ids, String... keys) {
    if (ToolsKit.isEmpty(ids)) {
        throw new NullPointerException("ids is null");
    }
    if (ToolsKit.isEmpty(keys)) {
        throw new NullPointerException("keys is null");
    }
    MongoQuery query = new MongoQuery();
    query.in(IdEntity.ID_FIELD, ids.toArray());
    DBObject dbo = new BasicDBObject();
    for (String key : keys) {
        dbo.put(key, 1);
    }
    DBObject update = new BasicDBObject(Operator.UNSET, dbo);
    WriteResult result = coll.updateMulti(query.getQueryObj(), update);
    return result.getN();
}
Also used : MongoQuery(com.duangframework.mongodb.common.MongoQuery)

Aggregations

MongoQuery (com.duangframework.mongodb.common.MongoQuery)5 EmptyNullException (com.duangframework.core.exceptions.EmptyNullException)2 ServiceException (com.duangframework.core.exceptions.ServiceException)2 MongoUpdate (com.duangframework.mongodb.common.MongoUpdate)2