Example usage for com.mongodb QueryBuilder get

List of usage examples for com.mongodb QueryBuilder get

Introduction

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

Prototype

public DBObject get() 

Source Link

Document

Creates a DBObject query to be used for the driver's find operations

Usage

From source file:com.linuxbox.enkive.audit.mongodb.MongoAuditService.java

License:Open Source License

/**
 * If there is a username, then we hint to use that index, as we expect
 * there to be many users compared to event codes. Thus it is likely that
 * generally filtering by users will reduce the result set by the greatest
 * amount. Of course some event codes are more prevalent than others, so
 * this won't always be the case./* w  w w .j  a  va2  s  .co  m*/
 * 
 * @param startTime
 *            all results must occur ON or AFTER this timestamp
 * @param endTime
 *            all results must occur BEFORE this timestamp
 * @throws AuditServiceException
 */
@Override
public List<AuditEntry> search(Integer eventCode, String userIdentifier, Date startTime, Date endTime)
        throws AuditServiceException {
    final QueryBuilder qb = QueryBuilder.start();
    String indexHint = TIMESTAMP_INDEX;

    if (eventCode != null) {
        qb.put(CODE_FIELD).is(eventCode);
        indexHint = CODE_TIMESTAMP_INDEX;
    }
    if (userIdentifier != null) {
        qb.put(USERNAME_FIELD).is(userIdentifier);
        indexHint = CODE_TIMESTAMP_INDEX;
    }
    if (startTime != null) {
        final int epochTime = (int) (startTime.getTime() / 1000);
        final BSONTimestamp timestamp = new BSONTimestamp(epochTime, 0);
        qb.put(TIMESTAMP_FIELD).greaterThanEquals(timestamp);
    }
    if (endTime != null) {
        final int epochTime = (int) (Math.ceil(endTime.getTime() / 1000.0));
        final BSONTimestamp timestamp = new BSONTimestamp(epochTime, 0);
        qb.put(TIMESTAMP_FIELD).lessThan(timestamp);
    }

    final DBCursor cursor = auditCollection.find(qb.get()).sort(DATE_BACKWARD_SORT).hint(indexHint);
    return dbCursortoAuditEntryList(cursor);
}

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   ww  w. j  a  v  a 2 s  . co 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.linuxbox.util.queueservice.mongodb.MongoQueueService.java

License:Open Source License

@Override
public QueueEntry dequeueByShardKey(int rangeLow, int rangeHigh) throws QueueServiceException {
    final QueryBuilder query = new QueryBuilder().and(STATUS_FIELD).is(STATUS_ENQUEUED).and(SHARD_KEY_FIELD)
            .greaterThanEquals(rangeLow).and(SHARD_KEY_FIELD).lessThan(rangeHigh);
    return dequeueHelper(query.get());
}

From source file:com.mlveda.dori.Webhook.java

public ArrayList<Collection> getCollectionList() {

    MongoDatabase db = MongoProvider.getInstance();

    //        MongoCollection<Product> products = db.getCollection("disneytoys.myshopify.com_products", Product.class);
    BasicDBObject document = new BasicDBObject();
    QueryBuilder qb = new QueryBuilder();
    qb.or(new QueryBuilder().put("handle").notEquals("frontpage").get(),
            new QueryBuilder().put("published_at").notEquals(null).get());
    document.putAll(qb.get());

    FindIterable<Document> coll = db.getCollection("dakshal.myshopify.com_collections")
            .find(and(ne("title", "Frontpage"), ne("published_at", null)));
    //        FindIterable<Document> coll = db.getCollection("disneytoys.myshopify.com_collection").find(document);

    //        FindIterable<Document> foundDocument = coll.find(and(ne("title", "Frontpage"), ne("published_at", null)));
    final Type collectionType = new TypeToken<ArrayList<Collection>>() {
    }.getType();/*from  w ww .  j a v a2  s . c om*/

    final ArrayList<Collection> collection = new ArrayList<>();

    System.out.println();
    System.out.println();

    System.out.println("document: " + document);
    coll.forEach(new Block<Document>() {
        @Override
        public void apply(Document document) {

            collection.add(gson.fromJson(gson.toJson(document), Collection.class));

            //                System.out.println("document: " + document);
            //                ArrayList<Collection> c = gson.fromJson(gson.toJson(document.get("collections")), collectionType);
            //                System.out.println("collection: " + c);
            //                if (!c.isEmpty()) {
            //                    collection.addAll(c);
            //                }
            //                collection.add(t);
        }
    });
    System.out.println("Collection: " + gson.toJson(collection));
    System.out.println();
    System.out.println();

    return collection;
}

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

License:Apache License

/**
 * {@inheritDoc}//from   ww  w  .  j  a  v  a 2 s.c  om
 * 
 * @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.mobileman.kuravis.core.services.treatment_review.impl.TempTreatmentReviewServiceImpl.java

License:Apache License

/** 
 * {@inheritDoc}/*from   w w w .  ja va2  s  .com*/
 * @see com.mobileman.kuravis.core.services.treatment_review.TempTreatmentReviewService#deleteAllTempTreatmentReviewsOfUser(java.lang.String, java.lang.String)
 */
@Override
public void deleteAllTempTreatmentReviewsOfUser(String userId, String email) {
    if (userId == null) {
        userId = "";
    }

    if (email == null) {
        email = "";
    }

    QueryBuilder query = QueryBuilder.start().or(new BasicDBObject("subscription.email", email),
            new BasicDBObject("author." + EntityUtils.ID, userId));
    getCollection(EntityUtils.TEMP_TREATMENT_REVIEW).remove(query.get());
}

From source file:com.mobileman.kuravis.core.services.user.impl.InvitationServiceImpl.java

License:Apache License

/** 
 * {@inheritDoc}/*  w  w w  .  j a  v a 2  s  .c  o  m*/
 * @see com.mobileman.kuravis.core.services.user.InvitationService#inviteeData(java.lang.String)
 */
@Override
public DBObject inviteeData(String email) {
    boolean userExists = userService.userExistsWithEmail(email);
    boolean invitationExists = count(new BasicDBObject("email", email)) > 0;
    boolean canBeregistered = invitationExists && !userExists;
    DBObject result = new BasicDBObject();
    result.put("canberegistered", canBeregistered);

    DBObject tempTreatmentReview = null;
    if (canBeregistered) {
        QueryBuilder query = QueryBuilder.start().put("subscription.email").is(email);
        List<DBObject> tempReviews = tempTreatmentReviewService.findAllByQuery(query.get());
        tempTreatmentReview = tempReviews.isEmpty() ? null : tempReviews.get(0);
    }

    result.put("treatmentReview", tempTreatmentReview);
    return result;
}

From source file:com.mobileman.kuravis.core.services.user.impl.ReminderServiceImpl.java

License:Apache License

/** 
 * {@inheritDoc}//from  w w w  .ja va 2s  .  c  o m
 * @see com.mobileman.kuravis.core.services.user.ReminderService#deleteAllRemindersOfEvent(java.lang.String)
 */
@Override
public void deleteAllRemindersOfEvent(String eventId) {
    QueryBuilder builder = QueryBuilder.start().put(Reminder.EVENT_ID).is(eventId);

    for (DBObject reminderData : findAllByQuery(builder.get())) {
        String quartzJobId = (String) reminderData.get(Reminder.QUARTZ_JOB_ID);
        try {
            this.schedulerFactoryBean.getScheduler().deleteJob(JobKey.jobKey(quartzJobId, ReminderJob.GROUP));
        } catch (SchedulerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        super.delete((String) reminderData.get(Reminder.ID));
    }
}

From source file:com.naryx.tagfusion.expression.function.string.serializejson.java

License:Open Source License

public void encodeJSON(StringBuilder out, cfData var, boolean serializeQueryByColumns, CaseType caseConversion,
        DateType datetype) throws dataNotSupportedException {

    if (var == null || var.getDataType() == cfData.CFNULLDATA) {
        out.append("null");

    } else if (var.getDataType() == cfData.CFSTRINGDATA) {

        /*/*from w  w  w  . j  a v  a 2  s  .c o  m*/
         * Encode Strings
         */
        out.append("\"" + encodeString(var.getString()) + "\"");

    } else if (var.getDataType() == cfData.CFBOOLEANDATA) {

        /*
         * Boolean Data
         */
        out.append(var.getBoolean() ? "true" : "false");

    } else if (var.getDataType() == cfData.CFDATEDATA) {

        /*
         * Encode Date
         */
        Date localeDate = (Date) ((cfDateData) var).getUnderlyingInstance();

        if (datetype == DateType.LONG)
            out.append("\"" + dateLongFormat.format(localeDate) + "\"");
        else if (datetype == DateType.HTTP)
            out.append("\"" + dateHTTPFormat.format(localeDate) + "\"");
        else if (datetype == DateType.JSON)
            out.append("\"" + dateJSONFormat.format(localeDate) + "\"");
        else if (datetype == DateType.MONGO)
            out.append("{$date:\"" + dateMongoFormat.format(localeDate) + "\"}");
        else
            out.append("\"" + var.getString() + "\"");

    } else if (cfData.isSimpleValue(var)) {

        /*
         * Encode numbers and booleans
         */
        out.append(var.getString());

    } else if (var.getDataType() == cfData.CFARRAYDATA) {

        /*
         * Encode Arrays
         */
        out.append("[");

        cfArrayData cfarraydata = (cfArrayData) var;
        for (int x = 0; x < cfarraydata.size(); x++) {
            encodeJSON(out, cfarraydata.getElement(x + 1), serializeQueryByColumns, caseConversion, datetype);

            if (x < cfarraydata.size() - 1)
                out.append(",");
        }
        out.append("]");

    } else if (var.getDataType() == cfData.CFSTRUCTDATA) {

        /*
         * Encode Structs
         */
        out.append("{");

        cfStructData cfstructdata = (cfStructData) var;
        Object[] keys = cfstructdata.keys();
        String key, value = null;
        for (int x = 0; x < keys.length; x++) {
            key = (String) keys[x];

            if (caseConversion == CaseType.LOWER)
                value = key.toLowerCase();
            else if (caseConversion == CaseType.UPPER)
                value = key.toUpperCase();
            else if (caseConversion == CaseType.MAINTAIN)
                value = key;

            out.append("\"" + encodeString(value) + "\"");
            out.append(":");
            encodeJSON(out, cfstructdata.getData(key), serializeQueryByColumns, caseConversion, datetype);

            if (x < keys.length - 1)
                out.append(",");
        }

        out.append("}");

    } else if (var.getDataType() == cfData.CFQUERYRESULTDATA) {

        /*
         * Encode Query
         */
        out.append("{");

        if (serializeQueryByColumns)
            encodeQueryByColumnsJSON(out, (cfQueryResultData) var, serializeQueryByColumns, caseConversion,
                    datetype);
        else
            encodeQueryByRowsJSON(out, (cfQueryResultData) var, serializeQueryByColumns, caseConversion,
                    datetype);

        out.append("}");

    } else if (var.getDataType() == cfData.CFJAVAOBJECTDATA) {
        Object o = ((cfJavaObjectData) var).getUnderlyingInstance();
        if (o instanceof org.bson.types.ObjectId)
            out.append("{$oid:\"" + ((org.bson.types.ObjectId) o).toString() + "\"}");
        else if (o instanceof java.util.regex.Pattern) {

            QueryBuilder qb = new QueryBuilder();
            qb.put("tmp");
            qb.regex((java.util.regex.Pattern) o);
            String json = qb.get().toString();
            json = json.substring(json.indexOf("{", 1), json.lastIndexOf("}"));
            out.append(json);

        } else
            out.append("\"" + encodeString(String.valueOf(o)) + "\"");
    } else {
        out.append("\"unsupported\"");
    }
}

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

License:Apache License

/**
 * Create query partition./*w w  w  . j  av  a  2s  .  c o  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();
}