Example usage for com.mongodb BasicDBObjectBuilder pop

List of usage examples for com.mongodb BasicDBObjectBuilder pop

Introduction

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

Prototype

public BasicDBObjectBuilder pop() 

Source Link

Document

Pops the active object, which means that the parent object becomes active

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();
    group.pop();/*from  w w w  .  j ava2 s .co  m*/

    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();
    group.pop();/*from  ww  w  .j ava  2 s  . c  o  m*/

    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();
    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();
    group.pop();//w w w  .  ja v  a 2  s .  co m

    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();
    group.push("count");
    group.add("$sum", 1);
    group.pop();/*from   w  ww  . j a  va 2  s. co  m*/
    group.pop();

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

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/>// w  ww .  j  a  va2s. c  o  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.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   w  w  w . j  a 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);
}

From source file:org.teiid.translator.mongodb.MongoDBSelectVisitor.java

License:Open Source License

private DBObject buildGeoNearFunction(Function function) {
    List<Expression> args = function.getParameters();

    // Column Name
    int paramIndex = 0;
    ColumnDetail column = getExpressionAlias(args.get(paramIndex++));

    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
    builder.push(column.documentFieldName);
    builder.push(function.getName());/*from   w w  w .  j  a  v  a 2s .c o  m*/
    builder.push("$geometry");//$NON-NLS-1$
    builder.add("type", SpatialType.Point.name());//$NON-NLS-1$

    // walk the co-ordinates
    append(args.get(paramIndex++));
    BasicDBList coordinates = new BasicDBList();
    coordinates.add(this.onGoingExpression.pop());
    builder.add("coordinates", coordinates); //$NON-NLS-1$

    // maxdistance
    append(args.get(paramIndex++));
    builder.pop().add("$maxDistance", this.onGoingExpression.pop()); //$NON-NLS-1$

    return builder.get();
}