Example usage for com.mongodb.client.model CountOptions CountOptions

List of usage examples for com.mongodb.client.model CountOptions CountOptions

Introduction

In this page you can find the example usage for com.mongodb.client.model CountOptions CountOptions.

Prototype

CountOptions

Source Link

Usage

From source file:io.djigger.collector.accessors.stackref.ThreadInfoAccessorImpl.java

License:Open Source License

public long count(Bson mongoQuery, Date from, Date to, long timeout, TimeUnit timeUnit)
        throws TimeoutException {
    mongoQuery = buildQuery(mongoQuery, from, to);

    CountOptions options = new CountOptions();
    options.maxTime(timeout, timeUnit);/*w  w w . j  a  v  a2 s.c o  m*/

    try {
        return threadInfoCollection.count(mongoQuery, options);
    } catch (MongoExecutionTimeoutException e) {
        throw new TimeoutException("Count exceeded time limit");
    }
}

From source file:org.eclipse.ditto.services.thingsearch.persistence.read.MongoThingsSearchPersistence.java

License:Open Source License

@Override
public Source<Long, NotUsed> count(final Query query, @Nullable final List<String> authorizationSubjectIds) {

    checkNotNull(query, "query");

    final BsonDocument queryFilter = getMongoFilter(query, authorizationSubjectIds);
    log.debug("count with query filter <{}>.", queryFilter);

    final CountOptions countOptions = new CountOptions().skip(query.getSkip()).limit(query.getLimit())
            .maxTime(maxQueryTime.getSeconds(), TimeUnit.SECONDS);

    return Source.fromPublisher(collection.count(queryFilter, countOptions))
            .mapError(handleMongoExecutionTimeExceededException()).log("count");
}

From source file:org.radarcns.mongo.util.MongoHelper.java

License:Apache License

/**
 * Finds whether document is available for given query parameters.
 * https://stackoverflow.com/a/8390458/822964 suggests find().limit(1).count(true) is the
 * optimal way to do it. In the Java driver, this is achieved by setting the count options.
 *
 * @param collection is the MongoDB that will be queried
 * @param projectName of the project/*from   w  w w.  j av  a  2 s.  co m*/
 * @param subjectId is the subjectID
 * @param sourceId is the sourceID
 * @param timeFrame is the time frame of the queried timewindow
 * @return a MongoDB cursor containing all documents from the query.
 */
public static boolean hasDataForSource(MongoCollection<Document> collection, String projectName,
        String subjectId, String sourceId, TimeFrame timeFrame) {
    createIndexIfNotAvailable(collection, indexProjectSubjectSourceTimestart);
    return collection.count(filterSource(projectName, subjectId, sourceId, timeFrame),
            new CountOptions().limit(1)) > 0;
}

From source file:step.core.accessors.Collection.java

License:Open Source License

public CollectionFind<Document> find(Bson query, SearchOrder order, Integer skip, Integer limit) {
    //      StringBuilder query = new StringBuilder();
    //      List<Object> parameters = new ArrayList<>();
    //      if(queryFragments!=null&&queryFragments.size()>0) {
    //         query.append("{$and:[");
    //         Iterator<String> it = queryFragments.iterator();
    //         while(it.hasNext()) {
    //            String criterium = it.next();
    //            query.append("{"+criterium+"}");
    //            if(it.hasNext()) {
    //               query.append(",");
    //            }
    //         }/*from  w  w w.  ja v a  2s  .c  om*/
    //         query.append("]}");
    //      }

    //      StringBuilder sort = new StringBuilder();
    //      sort.append("{").append(order.getAttributeName()).append(":")
    //         .append(Integer.toString(order.getOrder())).append("}");

    long count = collection.count();

    CountOptions option = new CountOptions();
    option.skip(0).limit(DEFAULT_LIMIT);
    long countResults = collection.count(query, option);

    FindIterable<Document> find = collection.find(query);
    if (order != null) {
        Document sortDoc = new Document(order.getAttributeName(), order.getOrder());
        find.sort(sortDoc);
    }
    if (skip != null) {
        find.skip(skip);
    }
    if (limit != null) {
        find.limit(limit);
    }
    return new CollectionFind<Document>(count, countResults, find.iterator());
}