Example usage for org.springframework.data.mongodb.core.query Query with

List of usage examples for org.springframework.data.mongodb.core.query Query with

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.query Query with.

Prototype

public Query with(Sort sort) 

Source Link

Document

Adds a Sort to the Query instance.

Usage

From source file:org.oncoblocks.centromere.mongodb.GenericMongoRepository.java

/**
 * {@link RepositoryOperations#findAll}// ww  w .  ja v  a  2  s .  co  m
 */
public Page<T> find(Iterable<QueryCriteria> queryCriterias, Pageable pageable) {
    Criteria criteria = MongoQueryUtils.getQueryFromQueryCriteria(queryCriterias);
    Query query = new Query();
    if (criteria != null) {
        query.addCriteria(criteria);
    }
    List<T> entities = mongoOperations.find(query.with(pageable), model);
    long count = count(queryCriterias);
    return new PageImpl<T>(entities, pageable, count);
}

From source file:com.traffitruck.service.MongoDAO.java

public List<Load> getLoadsForUser(String username) {
    Query findByUsername = new Query().addCriteria(Criteria.where("username").is(username));
    findByUsername.with(new Sort(Direction.DESC, "creationDate"));
    return mongoTemplate.find(findByUsername, Load.class);
}

From source file:com.github.camellabs.iot.cloudlet.geofencing.service.DefaultRouteService.java

@Override
public int analyzeRoutes(String client) {
    RouteGpsCoordinates lastRouteCoordinates = findLastRouteCoordinates(client);
    GpsCoordinates lastCoordinates = null;
    if (lastRouteCoordinates == null) {
        LOG.info("No GPS coordinates assigned to routes for client {}", client);
    } else {//  w  w  w. j  a v  a2 s  .  c o  m
        lastCoordinates = mongoTemplate.findById(lastRouteCoordinates.getCoordinatesId(), GpsCoordinates.class,
                GpsCoordinates.class.getSimpleName());
    }

    Query query = new Query();
    query.addCriteria(where("client").is(client));
    if (lastRouteCoordinates != null) {
        query.addCriteria(where("_id").gt(new ObjectId(lastRouteCoordinates.getCoordinatesId())));
    }
    query.limit(routeAnalysisBatchSize);
    query.with(new Sort(ASC, "_id"));
    List<GpsCoordinates> coordinatesToAnalyze = mongoTemplate.find(query, GpsCoordinates.class,
            GpsCoordinates.class.getSimpleName());
    for (GpsCoordinates coordinates : coordinatesToAnalyze) {
        String routeId;
        if (lastCoordinates == null || (TimeUnit.MILLISECONDS.toMinutes(
                coordinates.getTimestamp().getTime() - lastCoordinates.getTimestamp().getTime()) > 5)) {
            Route newRoute = createNewRoute(client);
            routeId = documentDriver.save(collectionName(newRoute.getClass()), pojoToMap(newRoute));
        } else {
            routeId = lastRouteCoordinates.getRouteId();
        }

        lastRouteCoordinates = new RouteGpsCoordinates(null, routeId, coordinates.getId(), client);
        mongoTemplate.save(lastRouteCoordinates, collectionName(RouteGpsCoordinates.class));
        lastCoordinates = coordinates;
    }
    return coordinatesToAnalyze.size();
}

From source file:com.seajas.search.profiler.service.repository.RepositoryService.java

/**
 * Retrieve a paged list of all resources within the repository.
 *
 * @param collection//  www .  j a  v a  2  s. c o m
 * @param sourceId
 * @param taxonomyMatch
 * @param startDate
 * @param endDate
 * @param pagerStart
 * @param pagerResults
 * @param parameters
 * @return RepositoryResult
 */
public RepositoryResult findResources(final String collection, final Integer sourceId,
        final String taxonomyMatch, final Date startDate, final Date endDate, final Integer pagerStart,
        final Integer pagerResults, final Map<String, String> parameters) {
    Query query = createQuery(false, collection, sourceId, taxonomyMatch, startDate, endDate, null, parameters);

    query.with(new Sort(Sort.Direction.DESC, "originalContent.dateSubmitted"));

    if (logger.isInfoEnabled())
        logger.info("About to count the number of results - which can potentially take a while - query = "
                + query.toString());

    // First perform a count

    Long totalResults = mongoTemplate.count(query, defaultCollection);

    if (logger.isInfoEnabled())
        logger.info("Counted " + totalResults + " result(s) to be retrieved from the storage back-end");

    // Then add paging parameters to the query

    query.skip(pagerStart);
    query.limit(pagerResults);

    // And build up the result

    List<RepositoryResource> results = new ArrayList<RepositoryResource>(pagerResults);
    List<CompositeEntry> entries = mongoTemplate.find(query, CompositeEntry.class, defaultCollection);

    for (CompositeEntry entry : entries)
        results.add(new RepositoryResource(entry.getOriginalContent().getUri().toString(),
                entry.getSource().getCollection(), entry.getSource().getId(),
                entry.getOriginalContent().getHostname(), entry.getOriginalContent().getDateSubmitted(),
                entry.getId().toString()));

    return new RepositoryResult(pagerStart, pagerResults, totalResults, results);
}

From source file:com.appleframework.monitor.model.Project.java

public List<MetricValue> findMetricData(String metricName) {

    Query query = fetchTimeQuery();
    query.addCriteria(Criteria.where("name").is(metricName));
    //query.sort().on(Constants.TIME_STAMP_FIELD_NAME, Order.ASCENDING);
    query.with(new Sort(Direction.ASC, Constants.TIME_STAMP_FIELD_NAME));
    logger.debug("find metric value by {} ,mongo={}", query.getQueryObject(), mongoUri);
    return fetchMongoTemplate().find(query, MetricValue.class, metricCollection);
}

From source file:io.gravitee.repository.mongodb.management.internal.event.EventMongoRepositoryImpl.java

@Override
public Page<EventMongo> search(Map<String, Object> values, long from, long to, int page, int size) {
    Query query = new Query();

    // set criteria query
    values.forEach((k, v) -> {/*from  w  w  w  . j a  v a2  s . c  o m*/
        if (v instanceof Collection) {
            query.addCriteria(Criteria.where(k).in((Collection) v));
        } else {
            query.addCriteria(Criteria.where(k).is(v));
        }
    });

    // set range query
    query.addCriteria(Criteria.where("updatedAt").gte(new Date(from)).lt(new Date(to)));

    // set sort by updated at
    query.with(new Sort(Sort.Direction.DESC, "updatedAt"));

    // set pageable
    query.with(new PageRequest(page, size));

    List<EventMongo> events = mongoTemplate.find(query, EventMongo.class);
    long total = mongoTemplate.count(query, EventMongo.class);

    Page<EventMongo> eventsPage = new Page<>(events, page, size, total);

    return eventsPage;
}

From source file:com.traffitruck.service.MongoDAO.java

public List<Truck> getTrucksWithoutImages() {
    Query findByUsername = new Query();
    findByUsername.fields().exclude("vehicleLicensePhoto");
    findByUsername.fields().exclude("truckPhoto");
    findByUsername.with(new Sort("creationDate"));
    return mongoTemplate.find(findByUsername, Truck.class);
}

From source file:com.gongpingjia.carplay.official.service.impl.OfficialApproveServiceImpl.java

/**
 * ?//from w  w  w .  j  av a 2  s  .c o  m
 *
 * @param type
 * @param status
 * @param start
 * @param end
 * @return
 */
private Query buildQueryParam(String type, String status, Long start, Long end, String applyUserId) {
    Criteria criteria = new Criteria();
    if (!StringUtils.isEmpty(applyUserId)) {
        criteria.and("applyUserId").is(applyUserId);
    }
    if (StringUtils.isNotEmpty(type)) {
        criteria.and("type").is(type);
    }
    if (StringUtils.isNotEmpty(status)) {
        criteria.and("status").is(status);
    }

    if (start != null && end != null) {
        criteria.and("applyTime").gte(start).lt(end + Constants.DAY_MILLISECONDS);
    } else if (end != null) {
        criteria.and("applyTime").lt(end + Constants.DAY_MILLISECONDS);
    } else if (start != null) {
        criteria.and("applyTime").gte(start);
    }

    Query query = Query.query(criteria);
    //        query.with(new Sort(new Sort.Order(Sort.Direction.ASC, "status")));
    query.with(new Sort(new Sort.Order(Sort.Direction.ASC, "applyTime")));
    return query;
}

From source file:com.traffitruck.service.MongoDAO.java

public Truck getTruckByIdWithoutImages(String id) {
    Query findTruckByIdQuery = new Query().addCriteria(Criteria.where("_id").is(id));
    findTruckByIdQuery.fields().exclude("vehicleLicensePhoto");
    findTruckByIdQuery.fields().exclude("truckPhoto");
    findTruckByIdQuery.with(new Sort("creationDate"));
    return mongoTemplate.findOne(findTruckByIdQuery, Truck.class);
}