Example usage for com.mongodb QueryBuilder and

List of usage examples for com.mongodb QueryBuilder and

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public QueryBuilder and(final DBObject... ands) 

Source Link

Document

Equivalent to an $and operand

Usage

From source file:com.bosscs.spark.mongodb.config.MongoDeepJobConfig.java

License:Apache License

/**
 * Filter query./*from w w  w  .j  a  v  a2  s.co  m*/
 *
 * @param filters the filters
 * @return the mongo deep job config
 */
public MongoDeepJobConfig<T> filterQuery(Filter[] filters) {

    if (filters.length > 0) {
        List<BasicDBObject> list = new ArrayList<>();

        QueryBuilder queryBuilder = QueryBuilder.start();
        for (int i = 0; i < filters.length; i++) {
            BasicDBObject bsonObject = new BasicDBObject();

            Filter filter = filters[i];
            if (filter.getFilterType().equals(FilterType.EQ)) {
                bsonObject.put(filter.getField(), filter.getValue());
            } else {
                bsonObject.put(filter.getField(), new BasicDBObject(
                        "$".concat(filter.getFilterType().getFilterTypeId().toLowerCase()), filter.getValue()));
            }

            list.add(bsonObject);
        }
        queryBuilder.and(list.toArray(new BasicDBObject[list.size()]).toString());

        filterQuery(queryBuilder);
    }
    return this;

}

From source file:com.bosscs.spark.mongodb.reader.MongoReader.java

License:Apache License

/**
 * Create query partition./*from  ww w .j a v a 2 s  . co m*/
 *
 * @param partition the partition
 * @return the dB object
 */
private DBObject createQueryPartition(MongoPartition partition) {

    QueryBuilder queryBuilderMin = QueryBuilder.start(partition.getKey());
    DBObject bsonObjectMin = queryBuilderMin.greaterThanEquals(partition.splitWrapper().getStartToken()).get();

    QueryBuilder queryBuilderMax = QueryBuilder.start(partition.getKey());
    DBObject bsonObjectMax = queryBuilderMax.lessThan(partition.splitWrapper().getEndToken()).get();

    QueryBuilder queryBuilder = QueryBuilder.start();
    if (partition.splitWrapper().getStartToken() != null) {
        queryBuilder.and(bsonObjectMin.toString());
    }

    if (partition.splitWrapper().getEndToken() != null) {
        queryBuilder.and(bsonObjectMax.toString());
    }

    LOG.debug("mongodb query " + queryBuilder.get());

    return queryBuilder.get();
}

From source file:com.bosscs.spark.mongodb.reader.MongoReader.java

License:Apache License

/**
 * Generate filter query./*from w ww . j a v  a 2  s . co m*/
 *
 * @param partition the partition
 * @return the dB object
 */
private DBObject generateFilterQuery(MongoPartition partition) {

    if (mongoDeepJobConfig.getQuery() != null) {
        QueryBuilder queryBuilder = QueryBuilder.start();
        queryBuilder.and(createQueryPartition(partition).toString() + mongoDeepJobConfig.getQuery());

        LOG.debug("mongodb query " + queryBuilder.get());

        return queryBuilder.get();
    }

    return createQueryPartition(partition);

}

From source file:com.linuxbox.enkive.docstore.mongogrid.MongoGridDocStoreService.java

License:Open Source License

private DBObject createUnindexedQuery(int shardKeyLow, int shardKeyHigh) {
    QueryBuilder protoQuery = new QueryBuilder();

    protoQuery.and(INDEX_STATUS_QUERY).is(STATUS_UNINDEXED);

    if (shardKeyLow > 0) {
        protoQuery.and(INDEX_SHARD_QUERY).greaterThanEquals(shardKeyLow);
    }/*from   w w  w. ja  v  a 2  s .c  o  m*/

    if (shardKeyHigh < INDEX_SHARD_KEY_COUNT) {
        protoQuery.and(INDEX_SHARD_QUERY).lessThan(shardKeyHigh);
    }

    final DBObject finalQuery = protoQuery.get();
    return finalQuery;
}

From source file:com.mobileman.kuravis.core.services.event.impl.EventServiceImpl.java

License:Apache License

/**
 * {@inheritDoc}/* www .  ja  va2 s.  c o m*/
 * 
 * @see com.mobileman.kuravis.core.services.event.EventService#findAllTreatmentEvents(java.lang.String, java.lang.String, java.lang.String, org.springframework.data.domain.Pageable)
 */
@Override
public List<DBObject> findAllTreatmentEvents(String userId, String diseaseId, String treatmentId,
        Pageable page) {
    QueryBuilder query = QueryBuilder.start().put(Event.EVENT_TYPE).is(EventType.TREATMENT.name());
    if (!StringUtils.isBlank(userId)) {
        query = query.and(Event.USER + "." + Event.ID).is(userId);
    }

    if (!StringUtils.isBlank(diseaseId)) {
        query = query.and(TreatmentEvent.DISEASE + "." + TreatmentEvent.ID).is(diseaseId);
    }

    if (!StringUtils.isBlank(treatmentId)) {
        query = query.and(TreatmentEvent.TREATMENT + "." + TreatmentEvent.ID).is(treatmentId);
    }

    List<DBObject> result = findAllByQuery(query.get(), page);
    return result;
}

From source file:com.stratio.deep.examples.java.factory.ReadingCellFromMongoDB.java

License:Apache License

public static void doMain(String[] args) {
    String job = "java:readingCellFromMongoDB";

    String host = "127.0.0.1:27017";

    String database = "test";
    String inputCollection = "input";

    // Creating the Deep Context where args are Spark Master and Job Name
    ContextProperties p = new ContextProperties(args);
    DeepSparkContext deepContext = new DeepSparkContext(p.getCluster(), job, p.getSparkHome(), p.getJars());

    QueryBuilder query = QueryBuilder.start();
    query.and("number").greaterThan(27).lessThan(30);

    DBObject bsonSort = new BasicDBObject();
    bsonSort.put("number", 1);

    DBObject bsonFields = new BasicDBObject();
    bsonFields.put("number", 1);
    bsonFields.put("text", 1);
    bsonFields.put("_id", 0);

    MongoDeepJobConfig inputConfigEntity = MongoConfigFactory.createMongoDB().host(host).database(database)
            .collection(inputCollection).createInputSplit(false).filterQuery(query).sort(bsonSort)
            .fields(bsonFields);//from  w ww  .  j a  va2s.c  o  m

    RDD inputRDDEntity = deepContext.createRDD(inputConfigEntity);

    LOG.info("count : " + inputRDDEntity.count());
    LOG.info("prints first cell  : " + inputRDDEntity.first());

    deepContext.stop();
}

From source file:com.stratio.deep.mongodb.config.MongoDeepJobConfig.java

License:Apache License

/**
 * Filter query.//  www. ja  v  a 2  s .  co  m
 *
 * @param filters the filters
 * @return the mongo deep job config
 */
public MongoDeepJobConfig<T> filterQuery(Filter[] filters) {

    if (filters.length > 0) {
        List<BasicDBObject> list = new ArrayList<>();

        QueryBuilder queryBuilder = QueryBuilder.start();
        for (int i = 0; i < filters.length; i++) {
            BasicDBObject bsonObject = new BasicDBObject();

            Filter filter = filters[i];
            if (filter.getFilterType().equals(FilterType.EQ)) {
                bsonObject.put(filter.getField(), filter.getValue());
            } else {
                bsonObject.put(filter.getField(), new BasicDBObject(
                        "$".concat(filter.getFilterType().getFilterTypeId().toLowerCase()), filter.getValue()));
            }

            list.add(bsonObject);
        }
        queryBuilder.and(list.toArray(new BasicDBObject[list.size()]));

        filterQuery(queryBuilder);
    }
    return this;

}

From source file:com.stratio.deep.mongodb.reader.MongoReader.java

License:Apache License

/**
 * Create query partition./*from ww  w . ja  v a2  s. co  m*/
 *
 * @param partition the partition
 * @return the dB object
 */
private DBObject createQueryPartition(MongoPartition partition) {

    QueryBuilder queryBuilderMin = QueryBuilder.start(partition.getKey());
    DBObject bsonObjectMin = queryBuilderMin.greaterThanEquals(partition.splitWrapper().getStartToken()).get();

    QueryBuilder queryBuilderMax = QueryBuilder.start(partition.getKey());
    DBObject bsonObjectMax = queryBuilderMax.lessThan(partition.splitWrapper().getEndToken()).get();

    QueryBuilder queryBuilder = QueryBuilder.start();
    if (partition.splitWrapper().getStartToken() != null) {
        queryBuilder.and(bsonObjectMin);
    }

    if (partition.splitWrapper().getEndToken() != null) {
        queryBuilder.and(bsonObjectMax);
    }

    LOG.debug("mongodb query " + queryBuilder.get());

    return queryBuilder.get();
}

From source file:com.streamreduce.storm.MongoClient.java

License:Apache License

/**
 * Returns the list of events in the Nodeable datastore between the Dates specified by the since and before
 * parameters.//from   w  ww.jav  a2s  .  c  o m
 *
 * @param since the date (exclusive) to get the events after.  A null means this parameter is ignored.
 * @param until the date (inclusive) to get the events before.  A null means this parameter is ignored.
 * @return the list of events or an empty list if there are none
 */
public List<BasicDBObject> getEvents(Date since, Date until) {
    DB connectionsDb = getDB("nodeablemsgdb");

    QueryBuilder queryBuilder = QueryBuilder.start();
    if (since != null) {
        queryBuilder.and("timestamp").greaterThan(since.getTime());
    }
    if (until != null) {
        queryBuilder.and("timestamp").lessThanEquals(until.getTime());
    }

    DBObject query = queryBuilder.get();
    return asList(connectionsDb.getCollection("eventStream").find(query));
}

From source file:it.sayservice.platform.smartplanner.otp.OTPManager.java

License:Apache License

private SymbolicRouteDayInfoHashCalendar findDayInfo(String router, String agencyId, String routeId,
        String date, boolean symbolic) {
    if (symbolic) {

        QueryBuilder qb = QueryBuilder.start();
        qb.and("routeId").is(routeId);
        qb.and("agencyId").is(agencyId);

        List<SymbolicRouteDayInfoHashCalendar> symbolicInfos = (List) storage.getObjectsByQuery(
                mongoRouterMapper.getMongoTemplateMap().get(router), qb.get(), Constants.AGENCY_INFO,
                SymbolicRouteDayInfoHashCalendar.class, null);

        SymbolicRouteDayInfoHashCalendar si = symbolicInfos.get(0);

        return si;

    } else {// w  w  w  . j a va2  s  .c o m
        return null;
    }
}