public static class Aggregate.Builder extends Object
Methods are provided for all existing pipeline operators and generic
step(java.lang.String, com.allanbank.mongodb.bson.DocumentAssignable)
methods are provided to support future pipeline operators
while in development or before the driver is updated.
This builder is intended to be used with the various support classes
including the Expressions
library. For example:
import static
com.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(); Aggregation.Builder builder = new Aggregation.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 | Field and Description |
---|---|
protected boolean |
myAllowDiskUsage
Set to true if the aggregation results should be allowed to spill to
disk.
|
protected int |
myBatchSize
The number of documents to be returned in each batch of results.
|
protected int |
myLimit
The total number of documents to be returned.
|
protected long |
myMaximumTimeMilliseconds
The maximum amount of time to allow the command to run.
|
protected ArrayBuilder |
myPipeline
The pipeline of operations to be applied.
|
protected ReadPreference |
myReadPreference
The read preference to use.
|
protected Version |
myRequiredVersion
The version required for the aggregation.
|
protected boolean |
myUseCursor
Set to true if the aggregation results should be returned as a
cursor.
|
Constructor and Description |
---|
Aggregate.Builder()
Creates a new Builder.
|
Modifier and Type | Method and Description |
---|---|
Aggregate.Builder |
allowDiskUsage()
Allows the aggregation command can spill to disk.
|
Aggregate.Builder |
allowDiskUsage(boolean allowDiskUsage)
Sets to true if the aggregation command can spill to disk.
|
Aggregate.Builder |
batchSize(int batchSize)
Sets the value of the number of documents to be returned in each
batch.
|
Aggregate |
build()
Constructs a new
Aggregate object from the state of the
builder. |
Aggregate.Builder |
cursorLimit(int limit)
Sets the value of the total number of documents to be returned.
|
Aggregate.Builder |
geoNear(AggregationGeoNear.Builder geoNear)
Adds a $geoNear operation to the pipeline to select
documents for the aggregation pipeline based on their relative
location to a set point.
|
Aggregate.Builder |
geoNear(AggregationGeoNear geoNear)
Adds a $geoNear operation to the pipeline to select
documents for the aggregation pipeline based on their relative
location to a set point.
|
Aggregate.Builder |
group(AggregationGroupId.Builder id,
AggregationGroupField... aggregations)
Adds a $group operation to the pipeline to aggregate
documents passing this point in the pipeline into a group of
documents.
|
Aggregate.Builder |
group(AggregationGroupId id,
AggregationGroupField... aggregations)
Adds a $group operation to the pipeline to aggregate
documents passing this point in the pipeline into a group of
documents.
|
Aggregate.Builder |
group(DocumentAssignable aggregations)
Adds a $group operation to the pipeline to aggregate
documents passing this point in the pipeline into a group of
documents.
|
Aggregate.Builder |
group(DocumentAssignable id,
AggregationGroupField... aggregations)
Adds a $group operation to the pipeline to aggregate
documents passing this point in the pipeline into a group of
documents.
|
Aggregate.Builder |
limit(int numberOfDocuments)
Adds a $limit operation to the pipeline to stop producing
documents passing this point in the pipeline once the limit of
documents is reached.
|
Aggregate.Builder |
limit(long numberOfDocuments)
Adds a $limit operation to the pipeline to stop producing
documents passing this point in the pipeline once the limit of
documents is reached.
|
Aggregate.Builder |
match(DocumentAssignable query)
Adds a $match operation to the pipeline to filter documents
passing this point in the pipeline.
|
Aggregate.Builder |
maximumTime(long timeLimit,
TimeUnit timeLimitUnits)
Sets the maximum number of milliseconds to allow the command to run
before aborting the request on the server.
|
Aggregate.Builder |
out(String collectionName)
Adds a $out operation to the pipeline to write all of the
output documents to the specified collection.
|
Aggregate.Builder |
project(AggregationProjectFields fields,
Element... elements)
Adds a $project operation to the pipeline to create a
projection of the documents passing this point in the pipeline.
|
Aggregate.Builder |
project(DocumentAssignable projection)
Adds a $project operation to the pipeline to create a
projection of the documents passing this point in the pipeline.
|
Aggregate.Builder |
redact(DocumentAssignable ifExpression,
RedactOption thenOption,
RedactOption elseOption)
Adds a $redact operation to potentially prune sub-documents
from the results.
|
Aggregate.Builder |
redact(Expression ifExpression,
RedactOption thenOption,
RedactOption elseOption)
Adds a $redact operation to potentially prune sub-documents
from the results.
|
Aggregate.Builder |
reset()
Resets the builder back to an empty pipeline.
|
Aggregate.Builder |
setAllowDiskUsage(boolean allowDiskUsage)
Sets to true if the aggregation command can spill to disk.
|
Aggregate.Builder |
setBatchSize(int batchSize)
Sets the value of the number of documents to be returned in each
batch.
|
Aggregate.Builder |
setCusorLimit(int limit)
Sets the value of the total number of documents to be returned.
|
Aggregate.Builder |
setMaximumTimeMilliseconds(long maximumTimeMilliseconds)
Sets the maximum number of milliseconds to allow the command to run
before aborting the request on the server.
|
Aggregate.Builder |
setReadPreference(ReadPreference readPreference)
Sets the
ReadPreference specifying which servers may be used
to execute the aggregation. |
Aggregate.Builder |
setUseCursor(boolean useCursor)
Sets to true if the aggregation results should be returned as a
cursor.
|
Aggregate.Builder |
skip(int numberOfDocuments)
Adds a $skip operation to the pipeline to skip the specified
number of documents before allowing any document past this point in
the pipeline.
|
Aggregate.Builder |
skip(long numberOfDocuments)
Adds a $skip operation to the pipeline to skip the specified
number of documents before allowing any document past this point in
the pipeline.
|
Aggregate.Builder |
sort(IntegerElement... sortFields)
Adds a $sort operation to sort the documents passing this
point based on the sort specification provided.
|
Aggregate.Builder |
sort(String... sortFields)
Adds a $sort operation to sort the documents passing this
point based on the sort fields provides in ascending order.
|
Aggregate.Builder |
step(String operator,
DocumentAssignable stepDocument)
Adds a generic step to the builder's pipeline.
|
Aggregate.Builder |
step(String operator,
double value)
Adds a generic step to the builder's pipeline.
|
Aggregate.Builder |
step(String operator,
Element... elements)
Adds a generic step to the builder's pipeline.
|
Aggregate.Builder |
step(String operator,
int value)
Adds a generic step to the builder's pipeline.
|
Aggregate.Builder |
step(String operator,
List<Element> elements)
Adds a generic step to the builder's pipeline.
|
Aggregate.Builder |
step(String operator,
long value)
Adds a generic step to the builder's pipeline.
|
Aggregate.Builder |
step(String operator,
String value)
Adds a generic step to the builder's pipeline.
|
String |
toString()
Return the JSON for the current pipeline that would be constructed by
the builder.
|
Aggregate.Builder |
unwind(String fieldName)
Adds a $unwind operation generate a document for each
element of the specified array field with the array replaced with the
value of the element.
|
Aggregate.Builder |
useCursor()
Sets that the results should be returned using a cursor.
|
Aggregate.Builder |
useCursor(boolean useCursor)
Sets to true if the aggregation results should be returned as a
cursor.
|
protected boolean myAllowDiskUsage
protected int myBatchSize
protected int myLimit
protected long myMaximumTimeMilliseconds
protected final ArrayBuilder myPipeline
protected ReadPreference myReadPreference
protected Version myRequiredVersion
protected boolean myUseCursor
public Aggregate.Builder allowDiskUsage()
This method delegates to allowDiskUsage(true)
.
This method also sets the builder to use a cursor to true.
public Aggregate.Builder allowDiskUsage(boolean allowDiskUsage)
This method delegates to setAllowDiskUsage(boolean)
.
This method also sets the builder to use a cursor to true.
allowDiskUsage
- The new value for if the aggregation command can spill to
disk.public Aggregate.Builder batchSize(int batchSize)
This method delegates to setBatchSize(int)
.
This method also sets the builder to use a cursor to true.
batchSize
- The new value for the number of documents to be returned
in each batch.public Aggregate build()
Aggregate
object from the state of the
builder.Aggregate
object.public Aggregate.Builder cursorLimit(int limit)
This method delegates to setCusorLimit(int)
.
This method also sets the builder to use a cursor to true.
limit
- The new value for the total number of documents to be
returned.public Aggregate.Builder geoNear(AggregationGeoNear geoNear)
import
com.allanbank.mongodb.builder.AggregationGeoNear
;Aggregate.Builder
builder = new Aggregation.Builder(); builder.geoNear( AggregationGeoNear.builder() .location( new Point( 1, 2 ) ) .distanceLocationField( "stats.distance" ) .limit( 5 ).build() );
geoNear
- The options for the GeoNear operation.public Aggregate.Builder geoNear(AggregationGeoNear.Builder geoNear)
import
com.allanbank.mongodb.builder.AggregationGeoNear
;Aggregate.Builder
builder = new Aggregation.Builder(); builder.geoNear( AggregationGeoNear.builder() .location( new Point( 1, 2 ) ) .distanceLocationField( "stats.distance" ) .limit( 5 ) );
geoNear
- The options for the GeoNear operation.public Aggregate.Builder group(AggregationGroupId id, AggregationGroupField... aggregations)
This method is intended to construct groups with simple dynamic or static id documents.
import static
com.allanbank.mongodb.builder.AggregationGroupId.id
; import staticcom.allanbank.mongodb.builder.AggregationGroupField.set
;Aggregate.Builder
builder = new Aggregation.Builder(); builder.group( id("$field1"), set("resultField1").uniqueValuesOf("$field2"), set("resultField2").max("$field3"), set("sum").sum("$field4") );
id
- The builder for the _id field to specify unique
groups.aggregations
- The specification for the group id and what fields to
aggregate in the form of a document.public Aggregate.Builder group(AggregationGroupId.Builder id, AggregationGroupField... aggregations)
This method is intended to construct groups with complex dynamic or
static id documents. The AggregationGroupId.Builder
implements the DocumentBuilder
for construction of arbitrary
complex _id documents.
import static
com.allanbank.mongodb.builder.AggregationGroupId.id
; import staticcom.allanbank.mongodb.builder.AggregationGroupField.set
;Aggregate.Builder
builder = new Aggregation.Builder(); builder.group( id().addField("$field1").addField("$field2"), set("resultField1").uniqueValuesOf("$field3"), set("resultField2").first("$field4"), set("count").count() );
id
- The builder for the _id field to specify unique
groups.aggregations
- The specification for the group id and what fields to
aggregate in the form of a document.public Aggregate.Builder group(DocumentAssignable aggregations)
aggregations
- The specification for the group id and what fields to
aggregate in the form of a document.public Aggregate.Builder group(DocumentAssignable id, AggregationGroupField... aggregations)
This method is intended to construct groups with complex dynamic or
static id documents. The AggregationGroupId.Builder
implements the DocumentBuilder
for construction of arbitrary
complex _id documents.
import static
com.allanbank.mongodb.builder.AggregationGroupId.id
; import staticcom.allanbank.mongodb.builder.AggregationGroupField.set
;Aggregate.Builder
builder = new Aggregation.Builder(); builder.group( id().addInteger("i", 1), set("resultField1").uniqueValuesOf("$field3"), set("resultField2").first("$field4"), set("count").count() );
id
- The builder for the _id field to specify unique
groups.aggregations
- The specification for the group id and what fields to
aggregate in the form of a document.public Aggregate.Builder limit(int numberOfDocuments)
numberOfDocuments
- The number of documents to allow past this point in the
pipeline.public Aggregate.Builder limit(long numberOfDocuments)
numberOfDocuments
- The number of documents to allow past this point in the
pipeline.public Aggregate.Builder match(DocumentAssignable query)
This method may be used with the QueryBuilder
to easily
specify the criteria to match against.
import static
com.allanbank.mongodb.builder.QueryBuilder.where
Aggregation.Builder builder = new Aggregation.Builder(); builder.match( where("f").greaterThan(23).lessThan(42).and("g").lessThan(3) ); ...
query
- The query to match documents against.public Aggregate.Builder maximumTime(long timeLimit, TimeUnit timeLimitUnits)
This method equivalent to setMaximumTimeMilliseconds(timeLimitUnits.toMillis(timeLimit)
.
timeLimit
- The new maximum amount of time to allow the command to
run.timeLimitUnits
- The units for the maximum amount of time to allow the
command to run.Aggregate.Builder
for method call chaining.public Aggregate.Builder out(String collectionName)
This method also forces the ReadPreference
to be
ReadPreference.PRIMARY
.
collectionName
- The name of the collection to output the results to.public Aggregate.Builder project(AggregationProjectFields fields, Element... elements)
This method is intended to be used with the
AggregationProjectFields
and
Expressions
static helper methods.
import static
com.allanbank.mongodb.builder.AggregationProjectFields.include
; import staticcom.allanbank.mongodb.builder.expression.Expressions.*
; Aggregation.Builder builder = new Aggregation.Builder(); ... builder.project( include("chr", "begin", "end", "calledPloidy"), set("window", multiply( divide( subtract( field("begin"), mod(field("begin"), constant(interval))), constant(interval)), constant(interval)))); ...
fields
- The fields to copy into the projected results.elements
- The computed elements based on Expressions
.public Aggregate.Builder project(DocumentAssignable projection)
projection
- The specification for the projection to perform.public Aggregate.Builder redact(DocumentAssignable ifExpression, RedactOption thenOption, RedactOption elseOption)
ifExpression
- The expression to evaluate to determine if the current
sub-document should be pruned or not.thenOption
- Operation to apply if the ifExpression
evaluates
to true.elseOption
- Operation to apply if the ifExpression
evaluates
to false.public Aggregate.Builder redact(Expression ifExpression, RedactOption thenOption, RedactOption elseOption)
ifExpression
- The expression to evaluate to determine if the current
sub-document should be pruned or not.thenOption
- Operation to apply if the ifExpression
evaluates
to true.elseOption
- Operation to apply if the ifExpression
evaluates
to false.public Aggregate.Builder reset()
public Aggregate.Builder setAllowDiskUsage(boolean allowDiskUsage)
This method also sets the builder to use a cursor to true.
allowDiskUsage
- The new value for if the aggregation command can spill to
disk.public Aggregate.Builder setBatchSize(int batchSize)
This method also sets the builder to use a cursor to true.
batchSize
- The new value for the number of documents to be returned
in each batch.public Aggregate.Builder setCusorLimit(int limit)
This method also sets the builder to use a cursor to true.
limit
- The new value for the total number of documents to be
returned.public Aggregate.Builder setMaximumTimeMilliseconds(long maximumTimeMilliseconds)
maximumTimeMilliseconds
- The new maximum number of milliseconds to allow the
command to run.Aggregate.Builder
for method call chaining.public Aggregate.Builder setReadPreference(ReadPreference readPreference)
ReadPreference
specifying which servers may be used
to execute the aggregation.
If not set or set to null
then the
MongoCollection
instance's ReadPreference
will be
used.
readPreference
- The read preferences specifying which servers may be used.MongoCollection.getReadPreference()
public Aggregate.Builder setUseCursor(boolean useCursor)
useCursor
- The new value for if the results should be returned via a
cursor.public Aggregate.Builder skip(int numberOfDocuments)
numberOfDocuments
- The number of documents to skip past before allowing any
documents to pass this point in the pipeline.public Aggregate.Builder skip(long numberOfDocuments)
numberOfDocuments
- The number of documents to skip past before allowing any
documents to pass this point in the pipeline.public Aggregate.Builder sort(IntegerElement... sortFields)
This method is intended to be used with the Sort
class's
static methods:
import static
com.allanbank.mongodb.builder.Sort.asc
; import staticcom.allanbank.mongodb.builder.Sort.desc
; Aggregation.Builder builder = new Aggregation.Builder(); builder.setSort( asc("f"), desc("g") ); ...
sortFields
- The sort fields to use.public Aggregate.Builder sort(String... sortFields)
sortFields
- The sort fields to use in ascending order.public Aggregate.Builder step(String operator, DocumentAssignable stepDocument)
operator
- The operator to add to the pipeline.stepDocument
- The document containing the details of the step to apply.public Aggregate.Builder step(String operator, double value)
operator
- The operator to add to the pipeline.value
- The value for the operator.public Aggregate.Builder step(String operator, Element... elements)
operator
- The operator to add to the pipeline.elements
- The elements containing the details of the step to apply.public Aggregate.Builder step(String operator, int value)
operator
- The operator to add to the pipeline.value
- The value for the operator.public Aggregate.Builder step(String operator, List<Element> elements)
operator
- The operator to add to the pipeline.elements
- The elements containing the details of the step to apply.public Aggregate.Builder step(String operator, long value)
operator
- The operator to add to the pipeline.value
- The value for the operator.public Aggregate.Builder step(String operator, String value)
operator
- The operator to add to the pipeline.value
- The value for the operator.public String toString()
public Aggregate.Builder unwind(String fieldName)
fieldName
- The name of the array field within the document to unwind.
This name must start with a '$'. If it does not a '$' will
be prepended to the field name..public Aggregate.Builder useCursor()
This method delegates to setUseCursor(true)
.
public Aggregate.Builder useCursor(boolean useCursor)
This method delegates to setUseCursor(boolean)
.
useCursor
- The new value for if the results should be returned via a
cursor.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.