Example usage for com.mongodb QueryOperators NE

List of usage examples for com.mongodb QueryOperators NE

Introduction

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

Prototype

String NE

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

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  w w.ja v  a  2s.  c  o m*/
 * @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: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 . jav  a2 s.  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 DBObject walkEcmPath(Operator op, Operand rvalue) {
    if (op != Operator.EQ && op != Operator.NOTEQ) {
        throw new QueryParseException(NXQL.ECM_PATH + " requires = or <> operator");
    }//  w w w.  j  av  a 2s  .  c  o  m
    if (!(rvalue instanceof StringLiteral)) {
        throw new QueryParseException(NXQL.ECM_PATH + " requires literal path as right argument");
    }
    String path = ((StringLiteral) rvalue).value;
    if (path.length() > 1 && path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }
    String id = pathResolver.getIdForPath(path);
    if (id == null) {
        // no such path
        // TODO XXX do better
        return new BasicDBObject(MONGODB_ID, "__nosuchid__");
    }
    if (op == Operator.EQ) {
        return new BasicDBObject(idKey, id);
    } else {
        return new BasicDBObject(idKey, new BasicDBObject(QueryOperators.NE, id));
    }
}

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

License:Apache License

protected DBObject walkAncestorId(Operator op, Operand rvalue) {
    if (op != Operator.EQ && op != Operator.NOTEQ) {
        throw new QueryParseException(NXQL.ECM_ANCESTORID + " requires = or <> operator");
    }//from  ww w. java  2  s.com
    if (!(rvalue instanceof StringLiteral)) {
        throw new QueryParseException(NXQL.ECM_ANCESTORID + " requires literal id as right argument");
    }
    String ancestorId = ((StringLiteral) rvalue).value;
    if (op == Operator.EQ) {
        return new BasicDBObject(DBSDocument.KEY_ANCESTOR_IDS, ancestorId);
    } else {
        return new BasicDBObject(DBSDocument.KEY_ANCESTOR_IDS,
                new BasicDBObject(QueryOperators.NE, ancestorId));
    }
}

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   ww  w  .  java  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 walkIsNotNull(Operand value) {
    FieldInfo fieldInfo = walkReference(value);
    return new FieldInfoDBObject(fieldInfo, new BasicDBObject(QueryOperators.NE, null));
}

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

License:Apache License

public DBObject walkNotEq(Operand lvalue, Operand rvalue) {
    FieldInfo fieldInfo = walkReference(lvalue);
    Object right = walkOperand(rvalue);
    if (isMixinTypes(fieldInfo)) {
        if (!(right instanceof String)) {
            throw new QueryParseException("Invalid NE rhs: " + rvalue);
        }/*from w w  w. j  av a  2  s  .  c  o m*/
        return walkMixinTypes(Collections.singletonList((String) right), false);
    }
    right = checkBoolean(fieldInfo, right);
    // TODO check list fields
    return new FieldInfoDBObject(fieldInfo, new BasicDBObject(QueryOperators.NE, right));
}

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

License:GNU General Public License

public BSONObject generateBSONObject(String propertyName, Object value, Context context) {
    BSONObject dbObject = new BasicDBObject(QueryOperators.NE, null);// ?null
    return new BasicDBObject(propertyName, dbObject);
}

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

License:GNU General Public License

protected BSONObject lengthBSONObject(String propertyName, int lengthValue) {
    Pattern pattern = Pattern.compile("^.{" + lengthValue + "}$", Pattern.MULTILINE);
    return new BasicDBObject(propertyName,
            new BasicDBObject(QueryOperators.NE, new BasicDBObject("$regex", pattern)));
}

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

License:GNU General Public License

public BSONObject generateBSONObject(String propertyName, Object value, Context context) {
    if (value != null && value.getClass().isArray()) {
        BSONObject object = new BasicDBObject();
        Object[] array = (Object[]) value;
        if (array.length == 2) {
            object.put(QueryOperators.GT, array[0]);
            object.put(QueryOperators.LT, array[1]);
        }//from w w w  . ja va2 s  . com
        return new BasicDBObject(propertyName, new BasicDBObject(QueryOperators.NE, object));
    }
    return null;
}