Skip to content

DATAMONGO-1552 Add $facet, $bucket and $bucketAuto aggregation stages #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

mp911de
Copy link
Member

@mp911de mp911de commented Dec 9, 2016

We now support multi-faceted aggregation and bucket operations via Aggregations. Bucket aggregation stages can be used standalone to accumulate documents of an aggregation in buckets or together with $facet to create multi-faceted categorizations.

BucketAutoOperation bucketPrice = bucketAuto(Multiply.valueOf("price").multiplyBy(10), 3)
		.withGranularity(Granularities.E12)    
		.andOutputCount().as("count")    
		.andOutput("title").push().as("titles")    
		.andOutputExpression("price * 10")    
		.sum().as("sum");

BucketOperation bucketYear = bucket("year")    
		.withBoundaries(1800, 1900, 2000).withDefaultBucket("Other").andOutputCount().as("count")    
		.andOutput("title").push().as("titles");

newAggregation(Art.class,
		project("title", "artist", "year", "price"),    
		facet(match(Criteria.where("price").exists(true)), bucketPrice).as("categorizeByPrice")    
				.and(bucketYear).as("categorizeByYear"));

translates to:

[
    { "$project": {  "title": 1, "artist": 1, "year": 1, "price": 1 } },
    { "$facet": {
        "categorizeByPrice": [
            { "$match": { "price": {  "$exists": true } } },
            { "$bucketAuto": {
                "buckets": 3,
                "granularity": "E12",
                "groupBy": { "$multiply": [ "$price", 10 ] },
                "output": {
                    "count": { "$sum": 1 },
                    "titles": { "$push": "$title" },
                    "sum": { "$sum": { "$multiply": [ "$price", 10 ] } } }
            } } ],
        "categorizeByYear": [ 
            { "$bucket": {
                "boundaries": [ 1800, 1900, 2000 ],
                "default": "Other",
                "groupBy": "$year",
                "output": {
                    "count": { "$sum": 1 },
                    "titles": { "$push": "$title" }
                } } } ]
        }
    }
]

Related ticket: DATAMONGO-1552

christophstrobl pushed a commit that referenced this pull request Dec 14, 2016
christophstrobl pushed a commit that referenced this pull request Dec 14, 2016
christophstrobl pushed a commit that referenced this pull request Dec 14, 2016
christophstrobl pushed a commit that referenced this pull request Dec 14, 2016
christophstrobl added a commit that referenced this pull request Dec 14, 2016
Updated doc, removed whitespaces, minor method wording changes.

Original Pull Request: #426
christophstrobl pushed a commit that referenced this pull request Dec 14, 2016
christophstrobl pushed a commit that referenced this pull request Dec 14, 2016
christophstrobl pushed a commit that referenced this pull request Dec 14, 2016
christophstrobl added a commit that referenced this pull request Dec 14, 2016
Updated doc, removed whitespaces, minor method wording changes.

Original Pull Request: #426
@christophstrobl christophstrobl deleted the issue/DATAMONGO-1552 branch December 14, 2016 12:27
mp911de added a commit that referenced this pull request Oct 26, 2017
mp911de added a commit that referenced this pull request Oct 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants