Search in sources :

Example 61 with Query

use of org.springframework.data.mongodb.core.query.Query in project main by JohnPeng739.

the class GeneralAccessorImpl method find.

/**
 * {@inheritDoc}
 *
 * @see GeneralAccessor#find(List, Class)
 */
@Override
public <T extends Base> List<T> find(List<ConditionTuple> tuples, Class<T> clazz) throws UserInterfaceDalErrorException {
    try {
        if (clazz.isInterface()) {
            clazz = EntityFactory.getEntityClass(clazz);
        }
        Query query;
        Criteria cd;
        switch(tuples.size()) {
            case 0:
                query = new Query();
                break;
            case 1:
                ConditionTuple tuple = tuples.get(0);
                cd = where(tuple.field).is(tuple.value);
                query = query(cd);
                break;
            default:
                cd = where(tuples.get(0).field).is(tuples.get(0).value);
                for (int index = 1; index < tuples.size(); index++) {
                    tuple = tuples.get(index);
                    cd.and(tuple.field).is(tuple.value);
                }
                query = query(cd);
                break;
        }
        return template.find(query, clazz);
    } catch (ClassNotFoundException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(String.format("Condition find entity[%s] fail, condition: %s.", clazz.getName(), StringUtils.merge(tuples, ",")), ex);
        }
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL);
    }
}
Also used : TextQuery(org.springframework.data.mongodb.core.query.TextQuery) Query(org.springframework.data.mongodb.core.query.Query) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) TextCriteria(org.springframework.data.mongodb.core.query.TextCriteria) Criteria(org.springframework.data.mongodb.core.query.Criteria)

Example 62 with Query

use of org.springframework.data.mongodb.core.query.Query in project main by JohnPeng739.

the class GeneralAccessorImpl method list.

/**
 * {@inheritDoc}
 *
 * @see GeneralAccessor#list(Pagination, Class, boolean)
 */
@Override
public <T extends Base> List<T> list(Pagination pagination, Class<T> clazz, boolean isValid) {
    if (pagination == null) {
        pagination = new Pagination();
    }
    try {
        if (clazz.isInterface()) {
            clazz = EntityFactory.getEntityClass(clazz);
        }
        pagination.setTotal((int) count(clazz, isValid));
        int skip = (pagination.getPage() - 1) * pagination.getSize();
        int limit = pagination.getSize();
        if (isValid) {
            return template.find(query(where("valid").is(true)).skip(skip).limit(limit), clazz);
        } else {
            return template.find(new Query().skip(skip).limit(limit), clazz);
        }
    } catch (ClassNotFoundException ex) {
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL);
    }
}
Also used : Pagination(org.mx.dal.Pagination) TextQuery(org.springframework.data.mongodb.core.query.TextQuery) Query(org.springframework.data.mongodb.core.query.Query) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException)

Example 63 with Query

use of org.springframework.data.mongodb.core.query.Query in project newton by muoncore.

the class MongoSagaRepository method clearInterests.

private void clearInterests(Saga saga) {
    Query ops = new Query();
    ops.addCriteria(Criteria.where("sagaId").is(saga.getId()));
    BulkWriteResult execute = mongoTemplate.bulkOps(BulkOperations.BulkMode.ORDERED, SagaInterest.class).remove(ops).execute();
    log.debug("Saga is complete, removed {} interests", execute.getRemovedCount());
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) BulkWriteResult(com.mongodb.BulkWriteResult)

Example 64 with Query

use of org.springframework.data.mongodb.core.query.Query in project newton by muoncore.

the class MongoSagaRepository method updateSagaCreated.

private void updateSagaCreated(Saga saga, NewtonEvent event) {
    Query query = new Query();
    query.addCriteria(Criteria.where("sagaId").is(saga.getId()));
    Update update = new Update();
    update.set("sagaClassName", saga.getClass().getName());
    update.set("sagaId", saga.getId());
    update.set("eventId", event.getId());
    mongoTemplate.upsert(query, update, SagaCreated.class);
}
Also used : Query(org.springframework.data.mongodb.core.query.Query) Update(org.springframework.data.mongodb.core.query.Update)

Example 65 with Query

use of org.springframework.data.mongodb.core.query.Query in project spring-data-document-examples by spring-projects.

the class AlbumsIntegrationTest method lookupAlbumsByTrackNameUsingJson.

@Test
public void lookupAlbumsByTrackNameUsingJson() throws Exception {
    Query query = parseQuery("{'tracks.name' : 'Wheels'}");
    assertSinglePursuitAlbum(query);
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.Test)

Aggregations

Query (org.springframework.data.mongodb.core.query.Query)690 Test (org.junit.jupiter.api.Test)391 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)259 NearQuery (org.springframework.data.mongodb.core.query.NearQuery)109 TextQuery (org.springframework.data.mongodb.core.query.TextQuery)91 Document (org.bson.Document)90 Update (org.springframework.data.mongodb.core.query.Update)83 Criteria (org.springframework.data.mongodb.core.query.Criteria)74 Test (org.junit.Test)72 lombok.val (lombok.val)50 ObjectId (org.bson.types.ObjectId)47 PartTree (org.springframework.data.repository.query.parser.PartTree)44 Sort (org.springframework.data.domain.Sort)41 StepVerifier (reactor.test.StepVerifier)39 Assertions (org.assertj.core.api.Assertions)30 Bson (org.bson.conversions.Bson)29 Mono (reactor.core.publisher.Mono)29 Point (org.springframework.data.geo.Point)28 Collation (org.springframework.data.mongodb.core.query.Collation)27 List (java.util.List)26