Example usage for com.mongodb QueryOperators LTE

List of usage examples for com.mongodb QueryOperators LTE

Introduction

In this page you can find the example usage for com.mongodb QueryOperators LTE.

Prototype

String LTE

To view the source code for com.mongodb QueryOperators LTE.

Click Source Link

Usage

From source file:fr.cnes.sitools.dataset.database.mongodb.OperatorMongoDB.java

License:Open Source License

/**
 * Get the operator value for a MongoDB query
 * /*from  w  ww.  ja v a  2 s.c  om*/
 * @param operator
 *          the {@link Operator}
 * @return the string representation of this operator for MongoDB
 */
public static String getOperatorValue(Operator operator) {
    String value;
    switch (operator) {
    case EQ:
        value = null;
        break;
    case GT:
        value = QueryOperators.GT;
        break;
    case GTE:
        value = QueryOperators.GTE;
        break;
    case IN:
        value = QueryOperators.IN;
        break;
    case LIKE:
        value = "LIKE";
        break;
    case LT:
        value = QueryOperators.LT;
        break;
    case LTE:
        value = QueryOperators.LTE;
        break;
    case NOTIN:
        value = QueryOperators.NIN;
        break;
    case GEO_OVERLAP:
        value = "$within";
        break;
    case NE:
        value = QueryOperators.NE;
        break;
    default:
        value = null;
    }
    return value;

}

From source file:jmockmongo.DefaultQueryHandler.java

License:Open Source License

public BSONObject[] handleQuery(String database, String collection, BSONObject command) {

    if ("system.indexes".equals(collection))
        return new BSONObject[0];

    // https://jira.mongodb.org/browse/SERVER-6078
    BSONObject sort = null;//from  w w  w .  j  a  v a2  s . c  o m
    if (command.containsField("query")) {
        Object q = command.get("query");
        if (q instanceof BSONObject) {
            BSONObject qq = (BSONObject) q;
            Unsupported.supportedFields(command, "query", "orderby");
            sort = BSONUtils.getObject(command, "orderby");
            command = qq;
            if (sort != null && sort.keySet().isEmpty())
                sort = null;
        }
    }

    MockDB db = mongo.getDB(database);
    if (db != null) {
        if (command.keySet().isEmpty())
            return findAll(db, database, collection, sort);
        Object id = null;
        Map<String, List<QueryPredicate>> filters = new HashMap<String, List<QueryPredicate>>();
        for (String field : command.keySet()) {
            if ("_id".equals(field)) {
                id = command.get(field);
                if (id instanceof BSONObject) {
                    BSONObject options = (BSONObject) id;

                    for (String s : options.keySet()) {
                        if (s.equals("$ref") || s.equals("$id"))
                            continue;
                        if (QueryOperators.GT.equals(s)) {
                            multiPut(filters, field, new GreaterThan(options.get(s)));
                            id = null;
                        } else if (QueryOperators.LT.equals(s)) {
                            multiPut(filters, field, new LowerThan(options.get(s)));
                            id = null;
                        } else if (QueryOperators.GTE.equals(s)) {
                            multiPut(filters, field, new GreaterThanOrEqual(options.get(s)));
                            id = null;
                        } else if (QueryOperators.LTE.equals(s)) {
                            multiPut(filters, field, new LowerThanOrEqual(options.get(s)));
                            id = null;
                        } else if (s.startsWith("$"))
                            throw new UnsupportedOperationException(
                                    s + " queries are not implemented:" + command);
                    }

                }
            } else {
                if (field.startsWith("$"))
                    throw new UnsupportedOperationException(field + " queries are not implemented:" + command);
                if (field.contains("."))
                    throw new UnsupportedOperationException(
                            "nested field queries are not implemented: " + command);
                Object value = command.get(field);
                if (value instanceof String || value instanceof ObjectId || value instanceof Long
                        || value instanceof Integer) {
                    multiPut(filters, field, new Equality(new Object[] { value }));
                } else if (value instanceof BSONObject) {
                    BSONObject options = (BSONObject) value;
                    for (String f : options.keySet()) {
                        if ("$in".equals(f)) {
                            multiPut(filters, field, new Equality(BSONUtils.values(options, f)));
                        } else if (QueryOperators.GT.equals(f)) {
                            multiPut(filters, field, new GreaterThan(options.get(f)));
                        } else if (QueryOperators.LT.equals(f)) {
                            multiPut(filters, field, new LowerThan(options.get(f)));
                        } else if (QueryOperators.GTE.equals(f)) {
                            multiPut(filters, field, new GreaterThanOrEqual(options.get(f)));
                        } else if (QueryOperators.LTE.equals(f)) {
                            multiPut(filters, field, new LowerThanOrEqual(options.get(f)));
                        } else {
                            throw new UnsupportedOperationException(
                                    "unsupported query " + f + " for " + field + ": " + options);
                        }
                    }
                } else
                    throw new UnsupportedOperationException("unsupported query for " + field + ": " + command);

            }
        }
        BSONObject[] all = null;
        if (id == null) {
            all = findAll(db, database, collection, sort);
        } else {
            MockDBCollection c = db.getCollection(collection);
            if (c != null) {
                BSONObject result = c.findOne(id);
                if (result != null)
                    all = new BSONObject[] { result };
            }
        }
        if (all != null && all.length > 0) {
            if (filters.isEmpty())
                return all;

            List<BSONObject> result = new ArrayList<BSONObject>(all.length);
            candidates: for (BSONObject x : all) {
                for (Map.Entry<String, List<QueryPredicate>> e : filters.entrySet()) {
                    Object xx = x.get(e.getKey());
                    for (QueryPredicate q : e.getValue())
                        if (!q.test(xx))
                            continue candidates;

                }
                result.add(x);
            }
            return result.toArray(new BSONObject[0]);
        }

    }

    return new BSONObject[0];
}

From source file:org.eclipse.emf.cdo.server.internal.mongodb.Commits.java

License:Open Source License

public void loadCommitInfos(CDOBranch branch, long startTime, long endTime,
        final CDOCommitInfoHandler handler) {
    if (endTime < CDOBranchPoint.UNSPECIFIED_DATE) {
        throw new IllegalArgumentException("Counting not supported");
    }//w  w w.ja v  a2 s. co m

    DBObject query = new BasicDBObject();

    if (branch != null && store.isBranching()) {
        query.put(COMMITS_BRANCH, branch.getID());
    }

    BasicDBList list = new BasicDBList();
    if (startTime != CDOBranchPoint.UNSPECIFIED_DATE) {
        list.add(new BasicDBObject(QueryOperators.GTE, startTime));
    }

    if (endTime != CDOBranchPoint.UNSPECIFIED_DATE) {
        list.add(new BasicDBObject(QueryOperators.LTE, endTime));
    }

    int size = list.size();
    if (size == 2) {
        query.put(COMMITS_ID, list);
    } else if (size == 1) {
        query.put(COMMITS_ID, list.get(0));
    }

    InternalRepository repository = store.getRepository();
    final InternalCDOBranchManager branchManager = repository.getBranchManager();
    final InternalCDOCommitInfoManager commitManager = repository.getCommitInfoManager();

    new Query<Object>(query) {
        @Override
        public Object execute() {
            return execute(collection.find(getRef()).sort(new BasicDBObject(COMMITS_ID, 1)));
        }

        @Override
        protected Object handleDoc(DBObject doc) {
            long time = (Long) doc.get(COMMITS_ID);
            Object value = doc.get(COMMITS_PREVIOUS);
            long previous = value == null ? 0L : (Long) value;

            CDOBranch commitBranch;
            if (store.isBranching()) {
                int branchID = (Integer) doc.get(COMMITS_BRANCH);
                commitBranch = branchManager.getBranch(branchID);
            } else {
                commitBranch = branchManager.getMainBranch();
            }

            String user = (String) doc.get(COMMITS_USER);
            String comment = (String) doc.get(COMMITS_COMMENT);

            CDOCommitInfo commitInfo = commitManager.createCommitInfo(commitBranch, time, previous, user,
                    comment, null);
            handler.handleCommitInfo(commitInfo);
            return null;
        }
    }.execute();
}

From source file:org.eclipselabs.mongoemf.query.simple.SimpleQueryEngine.java

License:Open Source License

private DBObject buildDBObjectQuery(Expression expression) {
    final DBObject dbObject = new BasicDBObject();

    if (expression != null) {
        new QuerySwitch<Object>() {
            Object getValue(Literal literal) {
                return literal.getValue() == null ? literal.getLiteralValue() : literal.getValue();
            }//from   ww w  .  j a  v  a 2 s.  co m

            @Override
            public Object caseBinaryOperation(BinaryOperation binaryOperation) {
                Expression leftOperand = binaryOperation.getLeftOperand();
                String operator = binaryOperation.getOperator();

                if ("==".equals(operator)) {
                    Expression rightOperand = binaryOperation.getRightOperand();
                    String property = ExpressionBuilder.toString(leftOperand);

                    if (Keywords.ID_KEY.equals(property)) {
                        dbObject.put(property, new ObjectId(((Literal) rightOperand).getLiteralValue()));
                    } else if (rightOperand instanceof Literal) {
                        dbObject.put(property, getValue((Literal) rightOperand));
                    } else if ("null".equals(ExpressionBuilder.toString(rightOperand))) {
                        DBObject notExists = new BasicDBObject();
                        notExists.put("$exists", Boolean.FALSE);
                        dbObject.put(property, notExists);
                    } else {
                        // TODO: What to do?
                    }
                } else if ("!=".equals(operator)) {
                    Expression rightOperand = binaryOperation.getRightOperand();
                    String property = ExpressionBuilder.toString(leftOperand);
                    if (rightOperand instanceof Literal) {
                        DBObject notEqual = new BasicDBObject();
                        notEqual.put("$ne", getValue((Literal) rightOperand));
                        dbObject.put(property, notEqual);
                    } else if ("null".equals(ExpressionBuilder.toString(rightOperand))) {
                        DBObject exists = new BasicDBObject();
                        exists.put("$exists", Boolean.TRUE);
                        dbObject.put(property, exists);
                    } else {
                        // TODO: What to do?
                    }
                } else if ("<".equals(operator) || "<=".equals(operator) || ">".equals(operator)
                        || ">=".equals(operator)) {
                    Expression rightOperand = binaryOperation.getRightOperand();
                    String property = ExpressionBuilder.toString(leftOperand);
                    if (rightOperand instanceof Literal) {
                        DBObject compare = new BasicDBObject();
                        compare.put(
                                "<".equals(operator) ? QueryOperators.LT
                                        : "<=".equals(operator) ? QueryOperators.LTE
                                                : ">".equals(operator) ? QueryOperators.GT : QueryOperators.GTE,
                                getValue((Literal) rightOperand));
                        dbObject.put(property, compare);
                    } else {
                        // TODO: What to do?
                    }
                } else if ("||".equals(operator)) {
                    DBObject leftObject = buildDBObjectQuery(leftOperand);
                    DBObject rightObject = buildDBObjectQuery(binaryOperation.getRightOperand());
                    @SuppressWarnings("unchecked")
                    List<Object> or = (List<Object>) leftObject.get("$or");
                    if (or != null) {
                        or.add(rightObject);
                        dbObject.putAll(leftObject);
                    } else {
                        or = new ArrayList<Object>();
                        or.add(leftObject);
                        or.add(rightObject);
                        dbObject.put("$or", or);
                    }
                } else if ("&&".equals(operator)) {
                    dbObject.putAll(buildDBObjectQuery(leftOperand));
                    DBObject rightObject = buildDBObjectQuery(binaryOperation.getRightOperand());
                    for (String field : rightObject.keySet()) {
                        Object rightValue = rightObject.get(field);
                        Object leftValue = dbObject.get(field);
                        if (leftValue instanceof DBObject) {
                            DBObject leftDBObject = (DBObject) leftValue;
                            if (rightValue instanceof DBObject) {
                                DBObject rightDBObject = (DBObject) rightValue;
                                if (leftDBObject.containsField("$nin") && rightDBObject.containsField("$ne")) {
                                    @SuppressWarnings("unchecked")
                                    List<Object> values = (List<Object>) leftDBObject.get("$nin");
                                    values.add(rightDBObject.get("$ne"));
                                } else if (leftDBObject.containsField("$ne")
                                        && rightDBObject.containsField("$ne")) {
                                    DBObject nin = new BasicDBObject();
                                    List<Object> values = new ArrayList<Object>();
                                    values.add(leftDBObject.get("$ne"));
                                    values.add(rightDBObject.get("$ne"));
                                    nin.put("$nin", values);
                                    dbObject.put(field, nin);
                                } else {
                                    leftDBObject.putAll(rightDBObject);
                                }
                            } else {
                                Object all = leftDBObject.get("$all");
                                if (all instanceof List<?>) {
                                    @SuppressWarnings("unchecked")
                                    List<Object> allList = (List<Object>) all;
                                    allList.add(rightValue);
                                } else {
                                    // What to do?
                                }
                            }
                        } else if (leftValue instanceof List<?>) {
                            @SuppressWarnings("unchecked")
                            List<Object> leftListValue = (List<Object>) leftValue;
                            if (rightValue instanceof List<?>) {
                                leftListValue.addAll((List<?>) rightValue);
                            } else {
                                leftListValue.add(rightValue);
                            }
                        } else if (leftValue != null) {
                            if (rightValue instanceof List<?>) {
                                @SuppressWarnings("unchecked")
                                List<Object> rightListValue = (List<Object>) rightValue;
                                rightListValue.add(0, leftValue);
                                dbObject.put(field, rightListValue);
                            } else {
                                List<Object> listValue = new ArrayList<Object>();
                                listValue.add(leftValue);
                                listValue.add(rightValue);
                                DBObject in = new BasicDBObject("$all", listValue);
                                dbObject.put(field, in);
                            }
                        } else {
                            dbObject.put(field, rightValue);
                        }
                    }
                }

                return null;
            }
        }.doSwitch(expression);
    }

    return dbObject;
}

From source file:org.fornax.cartridges.sculptor.framework.accessimpl.mongodb.MongoDbFindByConditionAccessImpl.java

License:Apache License

protected void makeCriterion(DBObject query, ConditionalCriteria crit, boolean not) {
    ConditionalCriteria.Operator operator = crit.getOperator();
    if (Operator.Equal.equals(operator)) {
        Object dbValue = toData(crit.getFirstOperant());
        if (not) {
            dbValue = new BasicDBObject(QueryOperators.NE, dbValue);
        }//from w  w  w  . j a va  2s  .co  m
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (Operator.Like.equals(operator)) {
        Pattern regex = regex(crit.getFirstOperant(), false);
        Object dbValue = wrapNot(not, regex);
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (Operator.IgnoreCaseLike.equals(operator)) {
        Pattern regex = regex(crit.getFirstOperant(), true);
        Object dbValue = wrapNot(not, regex);
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (Operator.In.equals(operator)) {
        Object dbValue = toData(crit.getFirstOperant());
        if (not) {
            dbValue = new BasicDBObject(QueryOperators.NIN, dbValue);
        } else {
            dbValue = new BasicDBObject(QueryOperators.IN, dbValue);
        }
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (Operator.LessThan.equals(operator)) {
        Object dbValue = toData(crit.getFirstOperant());
        if (not) {
            dbValue = new BasicDBObject(QueryOperators.GTE, dbValue);
        } else {
            dbValue = new BasicDBObject(QueryOperators.LT, dbValue);
        }
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (Operator.LessThanOrEqual.equals(operator)) {
        Object dbValue = toData(crit.getFirstOperant());
        if (not) {
            dbValue = new BasicDBObject(QueryOperators.GT, dbValue);
        } else {
            dbValue = new BasicDBObject(QueryOperators.LTE, dbValue);
        }
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (Operator.GreatThan.equals(operator)) {
        Object dbValue = toData(crit.getFirstOperant());
        if (not) {
            dbValue = new BasicDBObject(QueryOperators.LTE, dbValue);
        } else {
            dbValue = new BasicDBObject(QueryOperators.GT, dbValue);
        }
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (Operator.GreatThanOrEqual.equals(operator)) {
        Object dbValue = toData(crit.getFirstOperant());
        if (not) {
            dbValue = new BasicDBObject(QueryOperators.LT, dbValue);
        } else {
            dbValue = new BasicDBObject(QueryOperators.GTE, dbValue);
        }
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (Operator.IsNull.equals(operator)) {
        Object dbValue = null;
        if (not) {
            dbValue = new BasicDBObject(QueryOperators.NE, dbValue);
        }
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (Operator.IsNotNull.equals(operator)) {
        Object dbValue;
        if (not) {
            dbValue = null;
        } else {
            dbValue = new BasicDBObject(QueryOperators.NE, null);
        }
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (Operator.IsEmpty.equals(operator)) {
        Object dbValue = "";
        if (not) {
            dbValue = new BasicDBObject(QueryOperators.NE, dbValue);
        }
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (Operator.IsNotEmpty.equals(operator)) {
        Object dbValue;
        if (not) {
            dbValue = "";
        } else {
            dbValue = new BasicDBObject(QueryOperators.NE, "");
        }
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (not && Operator.Between.equals(operator)) {
        throw new UnsupportedOperationException("Not between condition not supported");
    } else if (Operator.Between.equals(operator)) {
        Object first = toData(crit.getFirstOperant());
        Object second = toData(crit.getSecondOperant());
        DBObject dbValue = new BasicDBObject();
        dbValue.put(QueryOperators.GTE, first);
        dbValue.put(QueryOperators.LTE, second);
        query.put(crit.getPropertyFullName(), dbValue);
    } else if (Operator.And.equals(operator)) {
        makeCriterion(query, (ConditionalCriteria) crit.getFirstOperant(), not);
        makeCriterion(query, (ConditionalCriteria) crit.getSecondOperant(), not);
    } else if (Operator.Not.equals(operator)) {
        makeCriterion(query, (ConditionalCriteria) crit.getFirstOperant(), !not);
    } else if (Operator.Or.equals(operator)) {
        throw new UnsupportedOperationException("Or condition not supported");
    }
}

From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBQueryBuilder.java

License:Apache License

protected Object pushDownNot(Object object) {
    if (!(object instanceof DBObject)) {
        throw new QueryParseException("Cannot do NOT on: " + object);
    }//from w  w  w .jav a  2s .  c  om
    DBObject ob = (DBObject) object;
    Set<String> keySet = ob.keySet();
    if (keySet.size() != 1) {
        throw new QueryParseException("Cannot do NOT on: " + ob);
    }
    String key = keySet.iterator().next();
    Object value = ob.get(key);
    if (!key.startsWith("$")) {
        if (value instanceof DBObject) {
            // push down inside dbobject
            return new BasicDBObject(key, pushDownNot(value));
        } else {
            // k = v -> k != v
            return new BasicDBObject(key, new BasicDBObject(QueryOperators.NE, value));
        }
    }
    if (QueryOperators.NE.equals(key)) {
        // NOT k != v -> k = v
        return value;
    }
    if (QueryOperators.NOT.equals(key)) {
        // NOT NOT v -> v
        return value;
    }
    if (QueryOperators.AND.equals(key) || QueryOperators.OR.equals(key)) {
        // boolean algebra
        // NOT (v1 AND v2) -> NOT v1 OR NOT v2
        // NOT (v1 OR v2) -> NOT v1 AND NOT v2
        String op = QueryOperators.AND.equals(key) ? QueryOperators.OR : QueryOperators.AND;
        List<Object> list = (List<Object>) value;
        for (int i = 0; i < list.size(); i++) {
            list.set(i, pushDownNot(list.get(i)));
        }
        return new BasicDBObject(op, list);
    }
    if (QueryOperators.IN.equals(key) || QueryOperators.NIN.equals(key)) {
        // boolean algebra
        // IN <-> NIN
        String op = QueryOperators.IN.equals(key) ? QueryOperators.NIN : QueryOperators.IN;
        return new BasicDBObject(op, value);
    }
    if (QueryOperators.LT.equals(key) || QueryOperators.GT.equals(key) || QueryOperators.LTE.equals(key)
            || QueryOperators.GTE.equals(key)) {
        // TODO use inverse operators?
        return new BasicDBObject(QueryOperators.NOT, ob);
    }
    throw new QueryParseException("Unknown operator for NOT: " + key);
}

From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBQueryBuilder.java

License:Apache License

public DBObject walkLtEq(Operand lvalue, Operand rvalue) {
    FieldInfo fieldInfo = walkReference(lvalue);
    Object right = walkOperand(rvalue);
    return new FieldInfoDBObject(fieldInfo, new BasicDBObject(QueryOperators.LTE, right));
}

From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBQueryBuilder.java

License:Apache License

public DBObject walkBetween(Operand lvalue, Operand rvalue, boolean positive) {
    LiteralList l = (LiteralList) rvalue;
    FieldInfo fieldInfo = walkReference(lvalue);
    Object left = walkOperand(l.get(0));
    Object right = walkOperand(l.get(1));
    if (positive) {
        DBObject range = new BasicDBObject();
        range.put(QueryOperators.GTE, left);
        range.put(QueryOperators.LTE, right);
        return new FieldInfoDBObject(fieldInfo, range);
    } else {//from   ww w  .j av  a 2s .  c  om
        DBObject a = new FieldInfoDBObject(fieldInfo, new BasicDBObject(QueryOperators.LT, left));
        DBObject b = new FieldInfoDBObject(fieldInfo, new BasicDBObject(QueryOperators.GT, right));
        return new BasicDBObject(QueryOperators.OR, Arrays.asList(a, b));
    }
}

From source file:org.tinygroup.mongodb.engine.comparemode.impl.LessOrEqualsCompareMode.java

License:GNU General Public License

public BSONObject generateBSONObject(String propertyName, Object value, Context context) {
    return new BasicDBObject(propertyName, new BasicDBObject(QueryOperators.LTE, value));
}

From source file:v7db.files.mongodb.QueryUtils.java

License:Open Source License

/**
 * makes a $gte/$lte range query (both ends inclusive)
 *///from ww  w.  ja  v a 2s  .  co m
static DBObject between(String field, Object from, Object to) {
    return new BasicDBObject(field, new BasicDBObject(QueryOperators.GTE, from).append(QueryOperators.LTE, to));
}