Example usage for com.mongodb QueryOperators OR

List of usage examples for com.mongodb QueryOperators OR

Introduction

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

Prototype

String OR

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

Click Source Link

Usage

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

License:Apache License

@Override
public <T extends Document> int remove(Collection<T> collection, Map<String, Map<Key, Condition>> toRemove) {
    log("remove", toRemove);
    int num = 0;//from  w  ww  .  j a  va 2s.  co m
    DBCollection dbCollection = getDBCollection(collection);
    long start = PERFLOG.start();
    try {
        List<String> batchIds = Lists.newArrayList();
        List<DBObject> batch = Lists.newArrayList();
        Iterator<Entry<String, Map<Key, Condition>>> it = toRemove.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, Map<Key, Condition>> entry = it.next();
            QueryBuilder query = createQueryForUpdate(entry.getKey(), entry.getValue());
            batchIds.add(entry.getKey());
            batch.add(query.get());
            if (!it.hasNext() || batch.size() == IN_CLAUSE_BATCH_SIZE) {
                DBObject q = new BasicDBObject();
                q.put(QueryOperators.OR, batch);
                try {
                    num += dbCollection.remove(q).getN();
                } catch (Exception e) {
                    throw DocumentStoreException.convert(e, "Remove failed for " + batch);
                } finally {
                    if (collection == Collection.NODES) {
                        invalidateCache(batchIds);
                    }
                }
                batchIds.clear();
                batch.clear();
            }
        }
    } finally {
        PERFLOG.end(start, 1, "remove keys={}", toRemove);
    }
    return num;
}

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  ww  .j  ava 2s .c o  m*/
    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 walkOr(Operand lvalue, Operand rvalue) {
    Object left = walkOperand(lvalue);
    Object right = walkOperand(rvalue);
    List<Object> list = new ArrayList<>(Arrays.asList(left, right));
    return new BasicDBObject(QueryOperators.OR, list);
}

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 {/*ww w  . j  a v a  2 s. com*/
        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.nuxeo.ecm.core.storage.mongodb.MongoDBQueryBuilder.java

License:Apache License

protected DBObject walkStartsWithNonPath(Operand lvalue, String path) {
    FieldInfo fieldInfo = walkReference(lvalue);
    DBObject eq = new FieldInfoDBObject(fieldInfo, path);
    // escape except alphanumeric and others not needing escaping
    String regex = path.replaceAll("([^a-zA-Z0-9 /])", "\\\\$1");
    Pattern pattern = Pattern.compile(regex + "/.*");
    DBObject like = new FieldInfoDBObject(fieldInfo, pattern);
    return new BasicDBObject(QueryOperators.OR, Arrays.asList(eq, like));
}

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

License:Apache License

/**
 * Matches the mixin types against a list of values.
 * <p>//from   w ww .ja  va  2 s  .co m
 * 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.tinygroup.mongodb.engine.MongoDbContext.java

License:GNU General Public License

public DBObject generateConditionObject(List<ConditionField> conditionFields) {
    DBObject conditionObject = new BasicDBObject();
    List<ConditionField> orLists = new ArrayList<ConditionField>();
    List<ConditionField> andLists = new ArrayList<ConditionField>();
    for (ConditionField conditionField : conditionFields) {
        if (isCollectionField(conditionField.getFieldId())) {
            if (conditionField.getConnectMode().equalsIgnoreCase(OR)) {
                orLists.add(conditionField);
            } else {
                andLists.add(conditionField);
            }/*from w  w w.  j a va2  s .c o  m*/

        }
    }
    if (!CollectionUtil.isEmpty(andLists)) {
        BasicDBList andList = new BasicDBList();
        for (ConditionField conditionField : andLists) {
            addCondition(andList, conditionField);
        }
        if (andList.size() > 0) {
            conditionObject.put(QueryOperators.AND, andList);
        }
    }

    if (!CollectionUtil.isEmpty(orLists)) {
        BasicDBList list = new BasicDBList();
        for (ConditionField conditionField : orLists) {
            addCondition(list, conditionField);
        }
        if (list.size() > 0) {
            conditionObject.put(QueryOperators.OR, list);
        }

    }
    return conditionObject;

}