MongoDB Java Driver Aggregate extension


  • David M. Carr

WARNING: This is an experimental extension. Between versions, there may be changes that are not backwards compatible. When there's a reasonable alternative to this extension bundled in the MongoDB Java Driver itself, all maintenance of this extension will cease.

This is an extension to the MongoDB Java Driver to allow for easier syntax for working with the Aggregation Framework. It is available for free under the Apache License, Version 2.0. Releases are available from the extension homepage and from Maven Central.

For example, without the extension, a simple aggregate command in Groovy might look like this:

def output = collection.aggregate(
        new BasicDBObject('$match', new BasicDBObject('_id.demoId': new ObjectId(demoId))),
        new BasicDBObject('$unwind', '$attributes'),
        new BasicDBObject('$match', new BasicDBObject('', attributeId)),
        new BasicDBObject('$group', new BasicDBObject('_id', '$attributes.value'))

Using the extension, it would look more like this:

def output = ( AggregateBuilder.aggregate(collection)
        .match('_id.demoId', new ObjectId(demoId))
        .match('', attributeId)
        .group('_id', '$attributes.value')

Further converted examples are available on the Wiki.

Release Notes

  • 0.1.3 (Release TBD)
    • Throw exception is in GroupBuilder if targetField value already set #4
  • 0.1.2 (Released 1-4-2013)
    • Add DBO to assist in creating DBObjects in Java
    • Add DBL to assist in creating lists in Java
  • 0.1.1 (Released 9-4-2012)

    • Requires MongoDB Java driver 2.9.0+
    • Add method using aggregate driver helper method
    • Fix "No target field for operand" error using GroupBuilder.start(...)
    • GroupOperators and PipelineOperators are now package-private
    • Add overloads to AggregateBuilder match and group methods to support common builders
  • 0.1.0 (Released 8-12-2012)

    • Initial release
    • Requires MongoDB Java driver 2.8.0+

Build Status