Example usage for com.mongodb BasicDBObjectBuilder add

List of usage examples for com.mongodb BasicDBObjectBuilder add

Introduction

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

Prototype

public BasicDBObjectBuilder add(final String key, final Object val) 

Source Link

Document

Same as append

Usage

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

License:Open Source License

public Iterator<DBObject> maxPrice() {
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("$group");
    builder.push("_id");
    builder.add("maker", "$manufacturer");
    builder.pop();/*from w  w w .  ja  v a2 s.com*/
    builder.push("maxprice");
    builder.add("$max", "$price");
    builder.pop();
    builder.pop();

    return col.aggregate(builder.get()).results().iterator();
}

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

License:Open Source License

public Iterator<DBObject> doubleGroupStages() {
    BasicDBObjectBuilder group_1 = new BasicDBObjectBuilder();
    group_1.push("$group");
    group_1.push("_id");
    group_1.add("class_id", "$class_id");
    group_1.add("student_id", "$student_id");
    group_1.pop();/*from   w  w  w . j  a  v a 2s . co m*/
    group_1.push("average");
    group_1.add("$avg", "$score");
    group_1.pop();
    group_1.pop();

    BasicDBObjectBuilder group_2 = new BasicDBObjectBuilder();
    group_2.push("$group");
    group_2.add("_id", "$_id.class_id");
    group_2.push("average");
    group_2.add("$avg", "$average");
    group_2.pop();
    group_2.pop();

    return col.aggregate(group_1.get(), group_2.get()).results().iterator();
}

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

License:Open Source License

public Iterator<DBObject> project() {
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("$project");
    builder.add("_id", 0);
    builder.push("maker");
    builder.add("$toLower", "$manufacturer");
    builder.pop();// www .ja  va 2s. c o m
    builder.push("details");
    builder.add("category", "$category");
    builder.push("price");
    builder.add("$multiply", new Object[] { "$price", 10 });
    builder.pop();
    builder.pop();
    builder.add("item", "$name");
    builder.pop();

    return col.aggregate(builder.get()).results().iterator();
}

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

License:Open Source License

public Iterator<DBObject> match() {
    BasicDBObjectBuilder match = buildMatchDBObject();
    BasicDBObjectBuilder group = new BasicDBObjectBuilder();
    group.push("$group");
    group.add("_id", "$city");
    group.push("population");
    group.add("$sum", "$pop");
    group.pop();/*from   www.ja va2 s .c o m*/
    group.push("zip_codes");
    group.add("$addToSet", "$_id");
    group.pop();
    group.pop();

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

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 ww w  . j a v a  2  s . co 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  ww.j ava2s  .  co  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();

    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();//from   ww w  .  j  av a  2s  .  c  o  m
    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();/*w ww. j  a  v a2 s  .  c  om*/
    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();//from w  w w . j  a va  2  s .  c om
    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.datasource.MongoQueryFactory.java

License:Open Source License

private static BasicDBObjectBuilder replaceParameters(Object[] parameters, SpaceDocumentMapper<DBObject> mapper,
        BasicDBObjectBuilder builder, Integer index) {

    BasicDBObjectBuilder newBuilder = BasicDBObjectBuilder.start();

    DBObject document = builder.get();/*from w  ww . ja va  2 s.c  om*/

    Iterator<String> iterator = document.keySet().iterator();

    while (iterator.hasNext()) {
        String field = iterator.next();
        Object ph = document.get(field);

        if (index >= parameters.length)
            return builder;

        if (ph instanceof String) {
            if (PARAM_PLACEHOLDER.equals(ph)) {
                Object p = mapper.toObject(parameters[index++]);

                if (p instanceof DBObject
                        && Constants.CUSTOM_BINARY.equals(((DBObject) p).get(Constants.TYPE))) {
                    newBuilder.add(field + "." + Constants.HASH, ((DBObject) p).get(Constants.HASH));
                } else {
                    newBuilder.add(field, p);
                }
            }
        } else if (ph instanceof Pattern) {
            Pattern p = (Pattern) ph;

            if (LIKE.equalsIgnoreCase(p.pattern())) {
                newBuilder.add(field, convertLikeExpression((String) parameters[index++]));
            } else if (RLIKE.equalsIgnoreCase(p.pattern())) {
                newBuilder.add(field, Pattern.compile((String) parameters[index++], Pattern.CASE_INSENSITIVE));
            }
        } else {
            DBObject element = (DBObject) ph;

            Object p = mapper.toObject(parameters[index]);

            if (p instanceof DBObject) {
                String t = (String) ((DBObject) p).get(Constants.TYPE);
                String op = element.keySet().iterator().next();

                if (Constants.CUSTOM_BINARY.equals(t) && !op.equals("$ne"))
                    return newBuilder;
            }

            BasicDBObjectBuilder doc = replaceParameters(parameters, mapper,
                    BasicDBObjectBuilder.start(element.toMap()), index);

            newBuilder.add(field, doc.get());
        }
    }
    return newBuilder;
}