Example usage for com.mongodb BasicDBObjectBuilder push

List of usage examples for com.mongodb BasicDBObjectBuilder push

Introduction

In this page you can find the example usage for com.mongodb BasicDBObjectBuilder push.

Prototype

public BasicDBObjectBuilder push(final String key) 

Source Link

Document

Creates an new empty object and inserts it into the current object with the given key.

Usage

From source file:com.examples.aggregation.AggregationExample.java

License:Open Source License

public Iterator<DBObject> sort() {
    BasicDBObjectBuilder match = buildMatchDBObject();

    BasicDBObjectBuilder group = new BasicDBObjectBuilder();
    group.push("$group");
    group.add("_id", "$city");
    group.push("population");
    group.add("$sum", "$pop");
    group.pop();/*from  w ww  . j a v  a  2s  .  c  o m*/
    group.pop();

    BasicDBObjectBuilder project = new BasicDBObjectBuilder();
    project.push("$project");
    project.add("_id", 0);
    project.add("city", "$_id");
    project.add("population", 1);
    project.pop();

    BasicDBObjectBuilder sort = new BasicDBObjectBuilder();
    sort.push("$sort");
    sort.add("population", -1);
    sort.pop();

    return col.aggregate(match.get(), group.get(), project.get(), sort.get()).results().iterator();
}

From source file:com.examples.aggregation.AggregationExample.java

License:Open Source License

public Iterator<DBObject> limitAndSkip() {
    BasicDBObjectBuilder match = buildMatchDBObject();

    BasicDBObjectBuilder group = new BasicDBObjectBuilder();
    group.push("$group");
    group.add("_id", "$city");
    group.push("population");
    group.add("$sum", "$pop");
    group.pop();//from w w w.ja  v a  2s .  com
    group.pop();

    BasicDBObjectBuilder project = new BasicDBObjectBuilder();
    project.push("$project");
    project.add("_id", 0);
    project.add("city", "$_id");
    project.add("population", 1);
    project.pop();

    BasicDBObjectBuilder sort = new BasicDBObjectBuilder();
    sort.push("$sort");
    sort.add("population", -1);
    sort.pop();

    BasicDBObject skip = new BasicDBObject("$skip", 10);
    BasicDBObject limit = new BasicDBObject("$limit", 5);

    return col.aggregate(match.get(), group.get(), project.get(), sort.get(), skip, limit).results().iterator();
}

From source file:com.examples.aggregation.AggregationExample.java

License:Open Source License

private BasicDBObjectBuilder buildMatchDBObject() {
    BasicDBObjectBuilder match = new BasicDBObjectBuilder();
    match.push("$match");
    match.add("state", "NY");
    match.pop();/* w ww.  j  a va2s  . c om*/
    return match;
}

From source file:com.examples.aggregation.AggregationExample.java

License:Open Source License

public Iterator<DBObject> unwind() {
    BasicDBObject unwind = new BasicDBObject("$unwind", "$tags");

    BasicDBObjectBuilder group = new BasicDBObjectBuilder();
    group.push("$group");
    group.add("_id", "$tags");
    group.push("count");
    group.add("$sum", 1);
    group.pop();//www .ja va2 s  . com
    group.pop();

    BasicDBObjectBuilder sort = new BasicDBObjectBuilder();
    sort.push("$sort");
    sort.add("count", -1);
    sort.pop();

    BasicDBObject limit = new BasicDBObject("$limit", 10);

    BasicDBObjectBuilder project = new BasicDBObjectBuilder();
    project.push("$project");
    project.add("_id", 0);
    project.add("tag", "$_id");
    project.add("count", 1);

    return col.aggregate(unwind, group.get(), sort.get(), limit, project.get()).results().iterator();
}

From source file:com.examples.aggregation.AggregationExample.java

License:Open Source License

public Iterator<DBObject> doubleUnwind() {

    BasicDBObject unwindSizes = new BasicDBObject("$unwind", "$sizes");
    BasicDBObject unwindColors = new BasicDBObject("$unwind", "$colors");

    BasicDBObjectBuilder group = new BasicDBObjectBuilder();
    group.push("$group");
    group.push("_id");
    group.add("size", "$sizes");
    group.add("color", "$colors");
    group.pop();//w w w  . jav  a2  s  . co m
    group.push("count");
    group.add("$sum", 1);
    group.pop();
    group.pop();

    return col.aggregate(unwindSizes, unwindColors, group.get()).results().iterator();
}

From source file:com.gigaspaces.persistency.MongoClientConnector.java

License:Open Source License

private static DBObject normalize(DBObject obj) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();

    Iterator<String> iterator = obj.keySet().iterator();
    builder.push("$set");
    while (iterator.hasNext()) {

        String key = iterator.next();

        if (Constants.ID_PROPERTY.equals(key))
            continue;

        Object value = obj.get(key);

        if (value == null)
            continue;

        builder.add(key, value);/*from   ww  w.  j  a v a2s .c  om*/
    }

    return builder.get();
}

From source file:com.heisenberg.mongo.MongoWorkflowInstanceStore.java

License:Apache License

public WorkflowInstanceImpl lockWorkflowInstance(WorkflowInstanceQueryImpl processInstanceQuery) {
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
    if (processInstanceQuery.workflowInstanceId != null) {
        builder.add(fields._id, new ObjectId(processInstanceQuery.workflowInstanceId));
    }/*from w ww .  j  a  va 2s .com*/
    if (processInstanceQuery.activityInstanceId != null) {
        builder.add(fields.activityInstances + "." + fields._id,
                new ObjectId(processInstanceQuery.activityInstanceId));
    }

    DBObject query = builder.push(fields.lock).add("$exists", false).pop().get();
    DBObject update = BasicDBObjectBuilder.start().push("$set").push(fields.lock)
            .add(fields.time, Time.now().toDate()).add(fields.owner, processEngine.getId()).pop().pop().get();
    DBObject retrieveFields = new BasicDBObject().append(fields.archivedActivityInstances, false);

    BasicDBObject dbProcessInstance = findAndModify(query, update, retrieveFields);
    if (dbProcessInstance == null) {
        return null;
    }

    WorkflowInstanceImpl workflowInstance = readProcessInstance(dbProcessInstance);
    workflowInstance.trackUpdates(false);
    return workflowInstance;
}

From source file:org.alfresco.bm.cm.FileFolderService.java

License:Open Source License

/**
 * Get a list of folders filtered by the number of child files and/or folders, returning
 * results sorted according to the parameters supplied.
 * <p/>//from  w  ww. j a  v a 2  s. co  m
 * Apart from the context, all parametes are optional.  However, for best performance,
 * do not mix the file and folder levels; the underlying query performance will be OK
 * but the sorting will not be ideal.
 * <p/>
 * The sort precedence is <b>folderCount-fileCount</b>.
 * 
 * @param context           the context in which the folder path is valid (mandatory)
 * @param minLevel          the minimum folder level to consider (inclusive, optional)
 * @param maxLevel          the maximum folder level to consider (inclusive, optional)
 * @param minFiles          the minimum number of files in the folder (inclusive, optional)
 * @param maxFiles          the maximum number of files in the folder (inclusive, optional)
 * @param skip              the number of entries to skip
 * @param limit             the number of entries to return
 * @return                  the folders with the correct number of children
 */
public List<FolderData> getFoldersByCounts(String context, Long minLevel, Long maxLevel, Long minFolders,
        Long maxFolders, Long minFiles, Long maxFiles, int skip, int limit) {
    if (context == null) {
        throw new IllegalArgumentException();
    }

    BasicDBObjectBuilder queryObjBuilder = BasicDBObjectBuilder.start();
    BasicDBObjectBuilder sortObjBuilder = BasicDBObjectBuilder.start();

    queryObjBuilder.add(FIELD_CONTEXT, context);
    if (minLevel != null || maxLevel != null) {
        queryObjBuilder.push(FIELD_LEVEL);
        {
            if (minLevel != null) {
                queryObjBuilder.add("$gte", minLevel);
            }
            if (maxLevel != null) {
                queryObjBuilder.add("$lte", maxLevel);
            }
            // No sorting by level!
        }
        queryObjBuilder.pop();
    }
    if (minFolders != null || maxFolders != null) {
        queryObjBuilder.push(FIELD_FOLDER_COUNT);
        {
            if (minFolders != null) {
                queryObjBuilder.add("$gte", minFolders);
            }
            if (maxFolders != null) {
                queryObjBuilder.add("$lte", maxFolders);
            }
            // We have to sort by the counts
            sortObjBuilder.add(FIELD_FOLDER_COUNT, 1);
        }
        queryObjBuilder.pop();
    }
    if (minFiles != null || maxFiles != null) {
        queryObjBuilder.push(FIELD_FILE_COUNT);
        {
            if (minFiles != null) {
                queryObjBuilder.add("$gte", minFiles);
            }
            if (maxFiles != null) {
                queryObjBuilder.add("$lte", maxFiles);
            }
            // We have to sort by the counts
            sortObjBuilder.add(FIELD_FILE_COUNT, 1);
        }
        queryObjBuilder.pop();
    }
    DBObject queryObj = queryObjBuilder.get();
    DBObject sortObj = sortObjBuilder.get();

    DBCursor cursor = collection.find(queryObj).sort(sortObj).skip(skip).limit(limit);
    List<FolderData> results = fromDBCursor(cursor);
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Found " + results.size() + " results for file counts: \n" + "   context:    " + context
                + "\n" + "   minLevel:   " + minLevel + "\n" + "   maxLevel:   " + maxLevel + "\n"
                + "   minFiles:   " + minFiles + "\n" + "   maxFiles:   " + maxFiles + "\n" + "   skip:       "
                + skip + "\n" + "   limit:      " + limit);
    }
    return results;
}

From source file:org.alfresco.bm.event.mongo.MongoEventService.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w  w  w  .  j  a  v a  2  s .  c  o m*/
 */
@Override
public Event nextEvent(String driverId, long latestScheduledTime) {
    // Build query
    BasicDBObjectBuilder qb = BasicDBObjectBuilder.start().push(Event.FIELD_SCHEDULED_TIME) // Must be scheduled to execute
            .add("$lte", new Date(latestScheduledTime)).pop().add(Event.FIELD_LOCK_OWNER, null) // Must not be locked
            .push(Event.FIELD_DATA_OWNER) // We must own the data it or it must be unowned
            .add("$in", new String[] { dataOwner, null }).pop();
    if (driverId != null) {
        qb.push(Event.FIELD_DRIVER) // Must be assigned to the given driver or must be unassigned
                .add("$in", new String[] { driverId, null }).pop();
    }
    DBObject queryObj = qb.get();
    // Build sort
    DBObject sortObj = BasicDBObjectBuilder.start().add(Event.FIELD_SCHEDULED_TIME, Integer.valueOf(1)).get();
    // Build update
    long now = System.currentTimeMillis();
    DBObject updateObj = BasicDBObjectBuilder.start().push("$set").add(Event.FIELD_LOCK_OWNER, dataOwner)
            .add(Event.FIELD_LOCK_TIME, new Date(now)).pop().get();

    DBObject oldObj = collection.findAndModify(queryObj, sortObj, updateObj);
    // Make sure we return the event, as modified
    Event event = null;
    if (oldObj != null) {
        event = convertDBObject(oldObj);
        event.setLockOwner(dataOwner);
        event.setLockTime(now);
    }

    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("\n" + "Fetched next event (no lock present): \n" + "   Latest scheduled time:  "
                + latestScheduledTime + "\n" + "   Driver ID:              " + driverId + "\n" + "   Event: "
                + event);
    }
    return event;
}

From source file:org.alfresco.bm.log.MongoLogService.java

License:Open Source License

@Override
public DBCursor getLogs(String driverId, String test, String testRun, LogLevel level, Long minTime,
        Long maxTime, int skip, int limit) {
    BasicDBObjectBuilder queryObjBuilder = BasicDBObjectBuilder.start();
    if (level != null) {
        queryObjBuilder.push(FIELD_LEVEL).add("$gte", level.getLevel()).pop();
    }//from  ww w .ja v  a2  s  . co  m
    if (driverId != null) {
        queryObjBuilder.add(FIELD_DRIVER_ID, driverId);
    }
    if (test != null) {
        queryObjBuilder.add(FIELD_TEST, test);
    }
    if (testRun != null) {
        queryObjBuilder.add(FIELD_TEST_RUN, testRun);
    }
    if (minTime != null || maxTime != null) {
        queryObjBuilder.push(FIELD_TIME);
        if (minTime != null) {
            queryObjBuilder.add("$gte", new Date(minTime));
        }
        if (maxTime != null) {
            queryObjBuilder.add("$lt", new Date(maxTime));
        }
        queryObjBuilder.pop();
    }
    DBObject queryObj = queryObjBuilder.get();
    DBObject sortObj = new BasicDBObject(FIELD_TIME, -1);
    DBObject fieldsObj = BasicDBObjectBuilder.start().add(FIELD_ID, false).add(FIELD_TIME, true)
            .add(FIELD_DRIVER_ID, true).add(FIELD_TEST, true).add(FIELD_TEST_RUN, true).add(FIELD_LEVEL, true)
            .add(FIELD_MSG, true).get();
    return collection.find(queryObj, fieldsObj).sort(sortObj).skip(skip).limit(limit);
}