Example usage for com.mongodb QueryOperators NIN

List of usage examples for com.mongodb QueryOperators NIN

Introduction

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

Prototype

String NIN

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

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
 * //  w ww . jav  a2 s. co 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);
        }/* w w  w . ja  v a  2  s  .  c o 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 .  j  a  v a 2s . com*/
    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 walkIn(Operand lvalue, Operand rvalue, boolean positive) {
    FieldInfo fieldInfo = walkReference(lvalue);
    Object right = walkOperand(rvalue);
    if (!(right instanceof List)) {
        throw new QueryParseException("Invalid IN, right hand side must be a list: " + rvalue);
    }/*from  w  w  w. java2  s  . c  o m*/
    if (isMixinTypes(fieldInfo)) {
        return walkMixinTypes((List<String>) right, positive);
    }
    // TODO check list fields
    List<Object> list = (List<Object>) right;
    return new FieldInfoDBObject(fieldInfo,
            new BasicDBObject(positive ? QueryOperators.IN : QueryOperators.NIN, list));
}

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

License:Apache License

/**
 * Matches the mixin types against a list of values.
 * <p>//ww w .jav  a2 s .com
 * Used for:
 * <ul>
 * <li>ecm:mixinTypes = 'Foo'
 * <li>ecm:mixinTypes != 'Foo'
 * <li>ecm:mixinTypes IN ('Foo', 'Bar')
 * <li>ecm:mixinTypes NOT IN ('Foo', 'Bar')
 * </ul>
 * <p>
 * ecm:mixinTypes IN ('Foo', 'Bar')
 *
 * <pre>
 * { "$or" : [ { "ecm:primaryType" : { "$in" : [ ... types with Foo or Bar ...]}} ,
 *             { "ecm:mixinTypes" : { "$in" : [ "Foo" , "Bar]}}]}
 * </pre>
 *
 * ecm:mixinTypes NOT IN ('Foo', 'Bar')
 * <p>
 *
 * <pre>
 * { "$and" : [ { "ecm:primaryType" : { "$in" : [ ... types without Foo nor Bar ...]}} ,
 *              { "ecm:mixinTypes" : { "$nin" : [ "Foo" , "Bar]}}]}
 * </pre>
 */
public DBObject walkMixinTypes(List<String> mixins, boolean include) {
    /*
     * Primary types that match.
     */
    Set<String> matchPrimaryTypes;
    if (include) {
        matchPrimaryTypes = new HashSet<>();
        for (String mixin : mixins) {
            matchPrimaryTypes.addAll(getMixinDocumentTypes(mixin));
        }
    } else {
        matchPrimaryTypes = new HashSet<>(getDocumentTypes());
        for (String mixin : mixins) {
            matchPrimaryTypes.removeAll(getMixinDocumentTypes(mixin));
        }
    }
    /*
     * Instance mixins that match.
     */
    Set<String> matchMixinTypes = new HashSet<>();
    for (String mixin : mixins) {
        if (!isNeverPerInstanceMixin(mixin)) {
            matchMixinTypes.add(mixin);
        }
    }
    /*
     * MongoDB query generation.
     */
    // match on primary type
    DBObject p = new BasicDBObject(DBSDocument.KEY_PRIMARY_TYPE,
            new BasicDBObject(QueryOperators.IN, matchPrimaryTypes));
    // match on mixin types
    // $in/$nin with an array matches if any/no element of the array matches
    String innin = include ? QueryOperators.IN : QueryOperators.NIN;
    DBObject m = new BasicDBObject(DBSDocument.KEY_MIXIN_TYPES, new BasicDBObject(innin, matchMixinTypes));
    // and/or between those
    String op = include ? QueryOperators.OR : QueryOperators.AND;
    return new BasicDBObject(op, Arrays.asList(p, m));
}

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

License:Apache License

protected void addIgnoredIds(DBObject query, Set<String> ignored) {
    if (!ignored.isEmpty()) {
        DBObject notInIds = new BasicDBObject(QueryOperators.NIN, new ArrayList<>(ignored));
        query.put(idKey, notInIds);/* w  ww  .ja v a2 s. co m*/
    }
}