Example usage for com.mongodb QueryBuilder is

List of usage examples for com.mongodb QueryBuilder is

Introduction

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

Prototype

public QueryBuilder is(final Object object) 

Source Link

Document

Equivalent of the find({key:value})

Usage

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://from  www  .ja v a  2  s. c  om
        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  www. ja  v a  2  s  . c  om
<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.sipfoundry.sipxconfig.admin.commserver.imdb.PermissionsTestIntegration.java

License:Contributor Agreement License

public void testAddUser() throws Exception {
    Group g = new Group();
    PermissionName.INTERNATIONAL_DIALING.setEnabled(g, false);
    PermissionName.LONG_DISTANCE_DIALING.setEnabled(g, false);
    PermissionName.TOLL_FREE_DIALING.setEnabled(g, false);
    PermissionName.LOCAL_DIALING.setEnabled(g, true);
    PermissionName.FREESWITH_VOICEMAIL.setEnabled(g, false);
    PermissionName.EXCHANGE_VOICEMAIL.setEnabled(g, true);

    m_testUser.addGroup(g);/*from w w  w  . ja  va  2 s  .com*/
    m_testUser.setUserName("goober");
    m_testUser.setUniqueId(1);
    m_permissionDataSet.generate(m_testUser, m_permissionDataSet.findOrCreate(m_testUser));

    assertObjectWithIdPresent(getEntityCollection(), "User1");
    assertObjectListFieldCount(getEntityCollection(), "User1", MongoConstants.PERMISSIONS, 8);
    QueryBuilder qb = QueryBuilder.start(MongoConstants.ID);
    qb.is("User1").and(MongoConstants.PERMISSIONS).size(4).and(MongoConstants.PERMISSIONS)
            .is(PermissionName.LOCAL_DIALING.getName()).is(PermissionName.VOICEMAIL.getName())
            .is(PermissionName.EXCHANGE_VOICEMAIL.getName()).is(PermissionName.MOBILE.getName());
    assertObjectPresent(getEntityCollection(), qb.get());
}

From source file:org.sipfoundry.sipxconfig.admin.commserver.imdb.PermissionsTestIntegration.java

License:Contributor Agreement License

public void testAuthCodePermissions() {
    InternalUser user = new InternalUser();
    user.setSipPassword("123");
    user.setPintoken("11");
    user.setPermissionManager(getPermissionManager());
    user.setPermission(PermissionName.NINEHUNDERED_DIALING, true);
    user.setPermission(PermissionName.INTERNATIONAL_DIALING, false);
    user.setPermission(PermissionName.LOCAL_DIALING, false);
    user.setPermission(PermissionName.LONG_DISTANCE_DIALING, false);
    user.setPermission(PermissionName.MOBILE, false);
    user.setPermission(PermissionName.TOLL_FREE_DIALING, false);

    AuthCode code = new AuthCode();
    code.setInternalUser(user);//from   w  w w  . j a v a  2  s.co m
    m_permissionDataSet.generate(code, m_permissionDataSet.findOrCreate(code));
    assertObjectWithIdPresent(getEntityCollection(), "AuthCode-1");
    QueryBuilder qb = QueryBuilder.start(MongoConstants.ID);
    qb.is("AuthCode-1").and(MongoConstants.PERMISSIONS).size(1).and(MongoConstants.PERMISSIONS)
            .is(PermissionName.NINEHUNDERED_DIALING.getName());
    assertObjectPresent(getEntityCollection(), qb.get());

    user.setPermission(PermissionName.NINEHUNDERED_DIALING, false);
    user.setPermission(PermissionName.INTERNATIONAL_DIALING, true);
    code.setInternalUser(user);
    m_permissionDataSet.generate(code, m_permissionDataSet.findOrCreate(code));
    qb.is("AuthCode-1").and(MongoConstants.PERMISSIONS).size(1).and(MongoConstants.PERMISSIONS)
            .is(PermissionName.INTERNATIONAL_DIALING.getName());
    assertObjectPresent(getEntityCollection(), qb.get());
}

From source file:org.sipfoundry.sipxconfig.admin.commserver.imdb.UserStaticTestIntegration.java

License:Contributor Agreement License

public void testGenerate() throws Exception {
    m_userstaticDataSet.generate(m_users.get(0), m_userstaticDataSet.findOrCreate(m_users.get(0)));
    m_userstaticDataSet.generate(m_users.get(1), m_userstaticDataSet.findOrCreate(m_users.get(1)));
    m_userstaticDataSet.generate(m_users.get(2), m_userstaticDataSet.findOrCreate(m_users.get(2)));
    assertCollectionCount(getEntityCollection(), 3);
    QueryBuilder qb = QueryBuilder.start(MongoConstants.ID);
    qb.is("User0").and(MongoConstants.STATIC + "." + MongoConstants.CONTACT)
            .is("sip:" + USER_DATA[0][4] + "@" + DOMAIN);
    assertObjectPresent(getEntityCollection(), qb.get());
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User0",
            MongoConstants.STATIC + "." + MongoConstants.CONTACT, "sip:" + USER_DATA[0][4] + "@" + DOMAIN);
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User0",
            MongoConstants.STATIC + "." + MongoConstants.TO_URI, "sip:" + USER_DATA[0][3] + "@" + DOMAIN);
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User1",
            MongoConstants.STATIC + "." + MongoConstants.EVENT, "message-summary");
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User2",
            MongoConstants.STATIC + "." + MongoConstants.FROM_URI, "sip:IVR@" + DOMAIN);
}

From source file:org.sipfoundry.sipxconfig.commserver.imdb.PermissionsTestIntegration.java

License:Contributor Agreement License

public void testAddUser() throws Exception {
    Group g = new Group();
    PermissionName.INTERNATIONAL_DIALING.setEnabled(g, false);
    PermissionName.LONG_DISTANCE_DIALING.setEnabled(g, false);
    PermissionName.TOLL_FREE_DIALING.setEnabled(g, false);
    PermissionName.LOCAL_DIALING.setEnabled(g, true);
    PermissionName.FREESWITH_VOICEMAIL.setEnabled(g, false);
    PermissionName.EXCHANGE_VOICEMAIL.setEnabled(g, true);

    m_testUser.addGroup(g);// w w  w  .  jav a 2  s. com
    m_testUser.setUserName("goober");
    m_testUser.setUniqueId(1);
    getReplicationManager().replicateEntity(m_testUser, DataSet.PERMISSION);

    assertObjectWithIdPresent(getEntityCollection(), "User1");
    assertObjectListFieldCount(getEntityCollection(), "User1", MongoConstants.PERMISSIONS, 8);
    QueryBuilder qb = QueryBuilder.start(MongoConstants.ID);
    qb.is("User1").and(MongoConstants.PERMISSIONS).size(4).and(MongoConstants.PERMISSIONS)
            .is(PermissionName.LOCAL_DIALING.getName()).is(PermissionName.VOICEMAIL.getName())
            .is(PermissionName.EXCHANGE_VOICEMAIL.getName()).is(PermissionName.MOBILE.getName());
    assertObjectPresent(getEntityCollection(), qb.get());
}

From source file:org.sipfoundry.sipxconfig.commserver.imdb.PermissionsTestIntegration.java

License:Contributor Agreement License

public void testAuthCodePermissions() {
    InternalUser user = new InternalUser();
    user.setSipPassword("123");
    user.setPintoken("11");
    user.setPermissionManager(getPermissionManager());
    user.setPermission(PermissionName.NINEHUNDERED_DIALING, true);
    user.setPermission(PermissionName.INTERNATIONAL_DIALING, false);
    user.setPermission(PermissionName.LOCAL_DIALING, false);
    user.setPermission(PermissionName.LONG_DISTANCE_DIALING, false);
    user.setPermission(PermissionName.MOBILE, false);
    user.setPermission(PermissionName.TOLL_FREE_DIALING, false);

    AuthCode code = new AuthCode();
    code.setInternalUser(user);//  w  w  w .  j  a v a  2s .  c om
    getReplicationManager().replicateEntity(code, DataSet.PERMISSION);
    assertObjectWithIdPresent(getEntityCollection(), "AuthCode-1");
    QueryBuilder qb = QueryBuilder.start(MongoConstants.ID);
    qb.is("AuthCode-1").and(MongoConstants.PERMISSIONS).size(1).and(MongoConstants.PERMISSIONS)
            .is(PermissionName.NINEHUNDERED_DIALING.getName());
    assertObjectPresent(getEntityCollection(), qb.get());

    user.setPermission(PermissionName.NINEHUNDERED_DIALING, false);
    user.setPermission(PermissionName.INTERNATIONAL_DIALING, true);
    code.setInternalUser(user);

    getReplicationManager().replicateEntity(code, DataSet.PERMISSION);
    qb.is("AuthCode-1").and(MongoConstants.PERMISSIONS).size(1).and(MongoConstants.PERMISSIONS)
            .is(PermissionName.INTERNATIONAL_DIALING.getName());
    assertObjectPresent(getEntityCollection(), qb.get());
}

From source file:org.sipfoundry.sipxconfig.commserver.imdb.UserStaticTestIntegration.java

License:Contributor Agreement License

public void testGenerate() throws Exception {
    getReplicationManager().replicateEntity(m_users.get(0), DataSet.USER_STATIC);
    getReplicationManager().replicateEntity(m_users.get(1), DataSet.USER_STATIC);
    getReplicationManager().replicateEntity(m_users.get(2), DataSet.USER_STATIC);
    getReplicationManager().replicateEntity(m_users.get(3), DataSet.USER_STATIC);
    assertCollectionCount(getEntityCollection(), 4);
    QueryBuilder qb = QueryBuilder.start(MongoConstants.ID);
    qb.is("User0").and(MongoConstants.STATIC + "." + MongoConstants.CONTACT)
            .is("sip:" + USER_DATA[0][4] + "@" + DOMAIN);
    assertObjectPresent(getEntityCollection(), qb.get());
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User0",
            MongoConstants.STATIC + "." + MongoConstants.CONTACT, "sip:" + USER_DATA[0][4] + "@" + DOMAIN);
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User0",
            MongoConstants.STATIC + "." + MongoConstants.TO_URI, "sip:" + USER_DATA[0][3] + "@" + DOMAIN);
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User1",
            MongoConstants.STATIC + "." + MongoConstants.EVENT, "message-summary");
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User2",
            MongoConstants.STATIC + "." + MongoConstants.FROM_URI, "sip:IVR@" + DOMAIN);
    assertObjectWithIdFieldValuePresent(getEntityCollection(), "User3",
            MongoConstants.STATIC + "." + MongoConstants.CONTACT, "sip:test@mwi.com");
}

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://from   w  w  w.j a va  2  s.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.teiid.translator.mongodb.MongoDBSelectVisitor.java

License:Open Source License

protected QueryBuilder buildIsNullQuery(IsNull obj, QueryBuilder query) {
    if (obj.isNegated()) {
        query.notEquals(null);//w w  w.ja  va2  s . c  o  m
    } else {
        query.is(null);
    }
    return query;
}