Example usage for com.mongodb QueryBuilder greaterThan

List of usage examples for com.mongodb QueryBuilder greaterThan

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder greaterThan.

Prototype

public QueryBuilder greaterThan(final Object object) 

Source Link

Document

Equivalent to the $gt operator

Usage

From source file:com.ikanow.aleph2.shared.crud.mongodb.utils.MongoDbUtils.java

License:Apache License

/** Creates the MongoDB clause from the QueryComponent inner object
 * @param field - the field used in the clause
 * @param operator_args - an operator enum and a pair of objects whose context depends on the operator
 * @return the MongoDB clause//from w ww  .j  a  v  a 2 s.co m
 */
protected static BasicDBObject operatorToMongoKey(final String field,
        final Tuple2<Operator, Tuple2<Object, Object>> operator_args) {
    return Patterns.match(operator_args).<BasicDBObject>andReturn()
            .when(op_args -> Operator.exists == op_args._1(),
                    op_args -> new BasicDBObject(field, new BasicDBObject("$exists", op_args._2()._1())))

            .when(op_args -> (Operator.any_of == op_args._1()),
                    op_args -> new BasicDBObject(field, new BasicDBObject("$in", op_args._2()._1())))
            .when(op_args -> (Operator.all_of == op_args._1()),
                    op_args -> new BasicDBObject(field, new BasicDBObject("$all", op_args._2()._1())))

            .when(op_args -> (Operator.equals == op_args._1()) && (null != op_args._2()._2()),
                    op_args -> new BasicDBObject(field, new BasicDBObject("$ne", op_args._2()._2())))
            .when(op_args -> (Operator.equals == op_args._1()),
                    op_args -> new BasicDBObject(field, op_args._2()._1()))

            .when(op_args -> Operator.range_open_open == op_args._1(), op_args -> {
                QueryBuilder qb = QueryBuilder.start(field);
                if (null != op_args._2()._1())
                    qb = qb.greaterThan(op_args._2()._1());
                if (null != op_args._2()._2())
                    qb = qb.lessThan(op_args._2()._2());
                return (BasicDBObject) qb.get();
            }).when(op_args -> Operator.range_open_closed == op_args._1(), op_args -> {
                QueryBuilder qb = QueryBuilder.start(field);
                if (null != op_args._2()._1())
                    qb = qb.greaterThan(op_args._2()._1());
                if (null != op_args._2()._2())
                    qb = qb.lessThanEquals(op_args._2()._2());
                return (BasicDBObject) qb.get();
            }).when(op_args -> Operator.range_closed_closed == op_args._1(), op_args -> {
                QueryBuilder qb = QueryBuilder.start(field);
                if (null != op_args._2()._1())
                    qb = qb.greaterThanEquals(op_args._2()._1());
                if (null != op_args._2()._2())
                    qb = qb.lessThanEquals(op_args._2()._2());
                return (BasicDBObject) qb.get();
            }).when(op_args -> Operator.range_closed_open == op_args._1(), op_args -> {
                QueryBuilder qb = QueryBuilder.start(field);
                if (null != op_args._2()._1())
                    qb = qb.greaterThanEquals(op_args._2()._1());
                if (null != op_args._2()._2())
                    qb = qb.lessThan(op_args._2()._2());
                return (BasicDBObject) qb.get();
            }).otherwise(op_args -> new BasicDBObject());
}

From source file:org.apache.gora.mongodb.filters.DefaultFactory.java

License:Apache License

protected QueryBuilder appendToBuilder(final QueryBuilder builder, final FilterOp filterOp,
        final List<Object> rawOperands) {
    List<String> operands = convertOperandsToString(rawOperands);
    switch (filterOp) {
    case EQUALS:/*  www  . j a va2  s  . c  o  m*/
        if (operands.size() == 1) {
            builder.is(operands.iterator().next());
        } else {
            builder.in(operands);
        }
        break;
    case NOT_EQUALS:
        if (operands.size() == 1) {
            builder.notEquals(operands.iterator().next());
        } else {
            builder.notIn(operands);
        }
        break;
    case LESS:
        builder.lessThan(operands);
        break;
    case LESS_OR_EQUAL:
        builder.lessThanEquals(operands);
        break;
    case GREATER:
        builder.greaterThan(operands);
        break;
    case GREATER_OR_EQUAL:
        builder.greaterThanEquals(operands);
        break;
    default:
        throw new IllegalArgumentException(filterOp + " no MongoDB equivalent yet");
    }
    return builder;
}

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java

License:Apache License

@SuppressWarnings("unchecked")
@Nonnull//from   w ww  .  j av a 2 s. com
<T extends Document> List<T> queryInternal(Collection<T> collection, String fromKey, String toKey,
        String indexedProperty, long startValue, int limit, long maxQueryTime) {
    log("query", fromKey, toKey, indexedProperty, startValue, limit);
    DBCollection dbCollection = getDBCollection(collection);
    QueryBuilder queryBuilder = QueryBuilder.start(Document.ID);
    queryBuilder.greaterThan(fromKey);
    queryBuilder.lessThan(toKey);

    DBObject hint = new BasicDBObject(NodeDocument.ID, 1);

    if (indexedProperty != null) {
        if (NodeDocument.DELETED_ONCE.equals(indexedProperty)) {
            if (startValue != 1) {
                throw new DocumentStoreException("unsupported value for property " + NodeDocument.DELETED_ONCE);
            }
            queryBuilder.and(indexedProperty);
            queryBuilder.is(true);
        } else {
            queryBuilder.and(indexedProperty);
            queryBuilder.greaterThanEquals(startValue);

            if (NodeDocument.MODIFIED_IN_SECS.equals(indexedProperty) && canUseModifiedTimeIdx(startValue)) {
                hint = new BasicDBObject(NodeDocument.MODIFIED_IN_SECS, -1);
            }
        }
    }
    DBObject query = queryBuilder.get();
    String parentId = Utils.getParentIdFromLowerLimit(fromKey);
    long lockTime = -1;
    final Stopwatch watch = startWatch();

    boolean isSlaveOk = false;
    int resultSize = 0;
    CacheChangesTracker cacheChangesTracker = null;
    if (parentId != null && collection == Collection.NODES) {
        cacheChangesTracker = nodesCache.registerTracker(fromKey, toKey);
    }
    try {
        DBCursor cursor = dbCollection.find(query).sort(BY_ID_ASC);
        if (!disableIndexHint && !hasModifiedIdCompoundIndex) {
            cursor.hint(hint);
        }
        if (maxQueryTime > 0) {
            // OAK-2614: set maxTime if maxQueryTimeMS > 0
            cursor.maxTime(maxQueryTime, TimeUnit.MILLISECONDS);
        }
        ReadPreference readPreference = getMongoReadPreference(collection, parentId, null,
                getDefaultReadPreference(collection));

        if (readPreference.isSlaveOk()) {
            isSlaveOk = true;
            LOG.trace("Routing call to secondary for fetching children from [{}] to [{}]", fromKey, toKey);
        }

        cursor.setReadPreference(readPreference);

        List<T> list;
        try {
            list = new ArrayList<T>();
            for (int i = 0; i < limit && cursor.hasNext(); i++) {
                DBObject o = cursor.next();
                T doc = convertFromDBObject(collection, o);
                list.add(doc);
            }
            resultSize = list.size();
        } finally {
            cursor.close();
        }

        if (cacheChangesTracker != null) {
            nodesCache.putNonConflictingDocs(cacheChangesTracker, (List<NodeDocument>) list);
        }

        return list;
    } finally {
        if (cacheChangesTracker != null) {
            cacheChangesTracker.close();
        }
        stats.doneQuery(watch.elapsed(TimeUnit.NANOSECONDS), collection, fromKey, toKey,
                indexedProperty != null, resultSize, lockTime, isSlaveOk);
    }
}

From source file:org.teiid.translator.mongodb.MongoDBSelectVisitor.java

License:Open Source License

protected void buildComparisionQuery(Comparison obj, Object rightExpr, QueryBuilder query) {
    switch (obj.getOperator()) {
    case EQ:/*  ww w  .  j  a v a2s  .c om*/
        query.is(rightExpr);
        break;
    case NE:
        query.notEquals(rightExpr);
        break;
    case LT:
        query.lessThan(rightExpr);
        break;
    case LE:
        query.lessThanEquals(rightExpr);
        break;
    case GT:
        query.greaterThan(rightExpr);
        break;
    case GE:
        query.greaterThanEquals(rightExpr);
        break;
    }
}

From source file:org.wrml.contrib.runtime.service.mongo.MongoService.java

License:Apache License

private void addQueryCriterion(final SearchCriterion searchCriterion, final QueryBuilder queryBuilder) {

    final ComparisonOperator comparisonOperator = searchCriterion.getComparisonOperator();
    final Object comparisonValue = searchCriterion.getComparisonValue();
    switch (comparisonOperator) {

    case containsAll: {
        queryBuilder.all(comparisonValue);
        break;/*ww  w . j av  a 2  s  .  c  om*/
    }

    case equalTo: {
        queryBuilder.equals(comparisonValue);
        break;
    }

    case equalToAny: {
        queryBuilder.in(comparisonValue);
        break;
    }

    case exists: {
        queryBuilder.exists(true);
        break;
    }

    case greaterThan: {
        queryBuilder.greaterThan(comparisonValue);
        break;
    }

    case greaterThanOrEqualTo: {
        queryBuilder.greaterThanEquals(comparisonValue);
        break;
    }

    case lessThan: {
        queryBuilder.lessThan(comparisonValue);
        break;
    }

    case lessThanOrEqualTo: {
        queryBuilder.lessThanEquals(comparisonValue);
        break;
    }

    case notEqualTo: {
        queryBuilder.notEquals(comparisonValue);
        break;
    }

    case notEqualToAny: {
        queryBuilder.notIn(comparisonValue);
        break;
    }

    case notExists: {
        queryBuilder.exists(false);
        break;
    }

    case regex: {
        final Pattern regexPattern = searchCriterion.getRegexPattern();
        if (regexPattern != null) {
            queryBuilder.regex(regexPattern);
        }

        break;
    }

    }
}