public class Aggregate extends Object
Instances of this class are constructed via the inner Aggregate.Builder class.
Due to the potential complexity of pipelines and the associated operators the
Builder is intended to be used with the various support classes
including the Expressions library. For example:
import staticcom.allanbank.mongodb.builder.AggregationGroupField.set; import staticcom.allanbank.mongodb.builder.AggregationGroupId.id; import staticcom.allanbank.mongodb.builder.AggregationProjectFields.includeWithoutId; import staticcom.allanbank.mongodb.builder.QueryBuilder.where; import staticcom.allanbank.mongodb.builder.Sort.asc; import staticcom.allanbank.mongodb.builder.Sort.desc; import staticcom.allanbank.mongodb.builder.expression.Expressions.field; import staticcom.allanbank.mongodb.builder.expression.Expressions.set; DocumentBuilder b1 = BuilderFactory.start(); DocumentBuilder b2 = BuilderFactory.start(); Aggregate.Builder builder = new Aggregate.Builder(); builder.match(where("state").notEqualTo("NZ")) .group(id().addField("state") .addField("city"), set("pop").sum("pop")) .sort(asc("pop")) .group(id("_id.state"), set("biggestcity").last("_id.city"), set("biggestpop").last("pop"), set("smallestcity").first("_id.city"), set("smallestpop").first("pop")) .project( includeWithoutId(), set("state", field("_id")), set("biggestCity", b1.add(set("name", field("biggestcity"))).add( set("pop", field("biggestpop")))), set("smallestCity", b2.add(set("name", field("smallestcity"))).add( set("pop", field("smallestpop"))))) .sort(desc("biggestCity.pop"));
| Modifier and Type | Class and Description |
|---|---|
static class |
Aggregate.Builder
Builder provides the ability to construct aggregate command pipelines.
|
| Modifier | Constructor and Description |
|---|---|
protected |
Aggregate(Aggregate.Builder builder)
Creates a new Aggregate.
|
| Modifier and Type | Method and Description |
|---|---|
List<Element> |
getPipeline()
Returns the pipeline of operations to apply.
|
ReadPreference |
getReadPreference()
Returns the
ReadPreference specifying which servers may be used
to execute the aggregation. |
protected Aggregate(Aggregate.Builder builder)
builder - The builder for the Aggregate instance.public List<Element> getPipeline()
public ReadPreference getReadPreference()
ReadPreference specifying which servers may be used
to execute the aggregation.
If null then the MongoCollection instance's
ReadPreference will be used.
MongoCollection.getReadPreference()Copyright © 2011-2013 Allanbank Consulting, Inc.. All Rights Reserved.