Further Examples

Methodology

This page will take examples from the Aggregation Framework Examples page in the MongoDB documentation and show how this library can be used to convert them into Java. See that page for the data file and further details about what these examples are supposed to do.

Map Syntax

If you are working in a language (such as Groovy) with a more compact Map syntax, you can substitute it for many uses of DBO.of. For this reason, examples are also given in Groovy when applicable.

Examples

States with Populations Over 10 Million

JavaScript:

db.zipcodes.aggregate( { $group :
                         { _id : "$state",
                           totalPop : { $sum : "$pop" } } },
                       { $match : {totalPop : { $gte : 10*1000*1000 } } } )

Java:

AggregationOutput output = AggregateBuilder.aggregate(collection)
    .group(GroupBuilder.start()
        .put("_id").to("$state")
        .put("totalPop").sum("$pop")
    )
    .match(QueryBuilder.start()
        .put("totalPop").greaterThanEquals(10 * 1000 * 1000)
        .get()
    )
    .run();

Average City Population by State

JavaScript:

db.zipcodes.aggregate( { $group :
                         { _id : { state : "$state", city : "$city" },
                           pop : { $sum : "$pop" } } },
                       { $group :
                       { _id : "$_id.state",
                         avgCityPop : { $avg : "$pop" } } } )

Java:

AggregationOutput output = AggregateBuilder.aggregate(collection)
    .group(GroupBuilder.start()
        .put("_id").to(DBO.of(
            "state", "$state",
            "city", "$city"
        ))
        .put("pop").sum("$pop")
    )
    .group(GroupBuilder.start()
        .put("_id").to("$_id.state")
        .put("avgCityPop").avg("$pop")
    )
    .run();

Groovy:

def output = ( AggregateBuilder.aggregate(collection)
    .group(GroupBuilder.start()
        .put('_id').to([state: '$state', city: '$city'])
        .put('pop').sum('$pop')
    )
    .group(GroupBuilder.start()
        .put('_id').to('$_id.state')
        .put('avgCityPop').avg('$pop')
    )
    .run()
)

Largest and Smallest Cities by State

JavaScript:

db.zipcodes.aggregate( { $group:
                         { _id: { state: "$state", city: "$city" },
                           pop: { $sum: "$pop" } } },
                       { $sort: { pop: 1 } },
                       { $group:
                         { _id : "$_id.state",
                           biggestCity:  { $last: "$_id.city" },
                           biggestPop:   { $last: "$pop" },
                           smallestCity: { $first: "$_id.city" },
                           smallestPop:  { $first: "$pop" } } },

                       // the following $project is optional, and
                       // modifies the output format.

                       { $project:
                         { _id: 0,
                           state: "$_id",
                           biggestCity:  { name: "$biggestCity",  pop: "$biggestPop" },
                           smallestCity: { name: "$smallestCity", pop: "$smallestPop" } } } )

Java:

AggregationOutput output = AggregateBuilder.aggregate(collection)
    .group(GroupBuilder.start()
        .put("_id").to(DBO.of(
            "state", "$state",
            "city", "$city"
        ))
        .put("pop").sum("$pop")
    )
    .sort(DBO.of("pop", 1))
    .group(GroupBuilder.start()
        .put("_id").to("$_id.state")
        .put("biggestCity").last("$_id.city")
        .put("biggestPop").last("$pop")
        .put("smallestCity").first("$_id.city")
        .put("smallestPop").first("$pop")
    )
    .project(
        DBO.of(
            "_id", 0,
            "state", "$_id",
            "biggestCity", DBO.of(
                "name", "$biggestCity",
                "pop", "$biggestPop"
            ),
            "smallestCity", DBO.of(
                "name", "$smallestCity",
                "pop", "$smallestPop"
            )
    ))
    .run();

Groovy:

def output = AggregateBuilder.aggregate(collection)
    .group(GroupBuilder.start()
        .put('_id').to([state: '$state', city: '$city'])
        .put('pop').sum('$pop')
    )
    .sort(['pop', 1])
    .group(GroupBuilder.start()
        .put('_id').to('$_id.state')
        .put('biggestCity').last('$_id.city')
        .put('biggestPop').last('$pop')
        .put('smallestCity').first('$_id.city')
        .put('smallestPop').first('$pop')
    )
    .project([
        _id: 0,
        state: '$_id',
        biggestCity: [ name: '$biggestCity', pop: '$biggestPop' ],
        smallestCity: [ name: '$smallestCity', pop: '$smallestPop' ]
    ])
    .run();

Updated

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.