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> simpleAggregation() {
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("$group");
    builder.add("_id", "$manufacturer");
    builder.push("num_products");
    builder.add("$sum", 1);
    builder.pop();/*ww  w . j a  va2  s.  c o  m*/
    builder.pop();

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

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

License:Open Source License

public Iterator<DBObject> compoundAggregation() {
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("$group");
    builder.push("_id");
    builder.add("manufacturer", "$manufacturer");
    builder.add("category", "$category");
    builder.pop();/*from  w ww  .ja va  2  s  . co  m*/
    builder.push("num_products");
    builder.add("$sum", 1);
    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> sumPrices() {
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("$group");
    builder.add("_id", "$manufacturer");
    builder.push("sum_prices");
    builder.add("$sum", "$price");
    builder.pop();/*ww  w . j  a v a 2  s  . c o m*/
    builder.pop();

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

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

License:Open Source License

public Iterator<DBObject> averagePrices() {
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("$group");
    builder.add("_id", "$category");
    builder.push("sum_prices");
    builder.add("$avg", "$price");
    builder.pop();//from   w w w. j  a  v  a  2 s  .  c o  m
    builder.pop();

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

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

License:Open Source License

public Iterator<DBObject> addToSet() {
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("$group");
    builder.push("_id");
    builder.add("maker", "$manufacturer");
    builder.pop();/*  www  . j ava  2s .  c  o  m*/
    builder.push("categories");
    builder.add("$addToSet", "$category");
    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> push() {
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("$group");
    builder.push("_id");
    builder.add("maker", "$manufacturer");
    builder.pop();//w w w.ja v  a2  s . c om
    builder.push("categories");
    builder.add("$push", "$category");
    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> maxPrice() {
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("$group");
    builder.push("_id");
    builder.add("maker", "$manufacturer");
    builder.pop();/*from  w w w.  j  a v a  2  s  . c  o  m*/
    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  ww.j av  a  2s. c  o  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();//w  w  w .ja  v  a  2 s .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 . j  a v  a 2  s. c om*/
    group.push("zip_codes");
    group.add("$addToSet", "$_id");
    group.pop();
    group.pop();

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