Skip to content

Commit d250f88

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-1552 - Update Documentation.
Original Pull Request: #426
1 parent bcb63b2 commit d250f88

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

src/main/asciidoc/new-features.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
== What's new in Spring Data MongoDB 1.10
66
* Support for `$min`, `$max` and `$slice` operators via `Update`.
77
* Support for `$cond` and `$ifNull` operators via `Aggregation`.
8+
* Multi-faceted aggregations using `$facet`, `$bucket` and `$bucketAuto` via `Aggregation`.
89

910
[[new-features.1-9-0]]
1011
== What's new in Spring Data MongoDB 1.9

src/main/asciidoc/reference/mongodb.adoc

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,7 +1674,7 @@ At the time of this writing we provide support for the following Aggregation Ope
16741674
[cols="2*"]
16751675
|===
16761676
| Pipeline Aggregation Operators
1677-
| count, geoNear, graphLookup, group, limit, lookup, match, project, replaceRoot, skip, sort, unwind
1677+
| bucket, bucketAuto, count, facet, geoNear, graphLookup, group, limit, lookup, match, project, replaceRoot, skip, sort, unwind
16781678

16791679
| Set Aggregation Operators
16801680
| setEquals, setIntersection, setUnion, setDifference, setIsSubset, anyElementTrue, allElementsTrue
@@ -1735,10 +1735,88 @@ Note that more examples for project operations can be found in the `AggregationT
17351735

17361736
Note that further details regarding the projection expressions can be found in the http://docs.mongodb.org/manual/reference/operator/aggregation/project/#pipe._S_project[corresponding section] of the MongoDB Aggregation Framework reference documentation.
17371737

1738+
[[mongo.aggregation.facet]]
1739+
=== Faceted classification
1740+
1741+
MongoDB supports as of Version 3.4 faceted classification using the Aggregation Framework. A faceted classification uses semantic categories, either general or subject-specific, that are combined to create the full classification entry. Documents flowing through the aggregation pipeline are classificated into buckets. A multi-faceted classification enables various aggregations on the same set of input documents, without needing to retrieve the input documents multiple times.
1742+
1743+
==== Buckets
1744+
1745+
Bucket operations categorize incoming documents into groups, called buckets, based on a specified expression and bucket boundaries. Bucket operations require a grouping field or grouping expression. They can be defined via the `bucket()`/`bucketAuto()` methods of the `Aggregate` class. `BucketOperation` and `BucketAutoOperation` can expose accumulations based on aggregation expressions for input documents. The bucket operation can be extended with additional parameters through a fluent API via the `with…()` methods, the `andOutput(String)` method and aliased via the `as(String)` method. Each bucket is represented as a document in the output.
1746+
1747+
`BucketOperation` takes a defined set of boundaries to group incoming documents into these categories. Boundaries are required to be sorted.
1748+
1749+
.Bucket operation examples
1750+
====
1751+
[source,java]
1752+
----
1753+
// will generate {$bucket: {groupBy: $price, boundaries: [0, 100, 400]}}
1754+
bucket("price").withBoundaries(0, 100, 400);
1755+
1756+
// will generate {$bucket: {groupBy: $price, default: "Other" boundaries: [0, 100]}}
1757+
bucket("price").withBoundaries(0, 100).withDefault("Other");
1758+
1759+
// will generate {$bucket: {groupBy: $price, boundaries: [0, 100], output: { count: { $sum: 1}}}}
1760+
bucket("price").withBoundaries(0, 100).andOutputCount().as("count");
1761+
1762+
// will generate {$bucket: {groupBy: $price, boundaries: [0, 100], 5, output: { titles: { $push: "$title"}}}
1763+
bucket("price").withBoundaries(0, 100).andOutput("title").push().as("titles");
1764+
----
1765+
====
1766+
1767+
`BucketAutoOperation` determines boundaries itself in an attempt to evenly distribute documents into a specified number of buckets. `BucketAutoOperation` optionally takes a granularity specifies the https://en.wikipedia.org/wiki/Preferred_number[preferred number] series to use to ensure that the calculated boundary edges end on preferred round numbers or their powers of 10.
1768+
1769+
.Bucket operation examples
1770+
====
1771+
[source,java]
1772+
----
1773+
// will generate {$bucketAuto: {groupBy: $price, buckets: 5}}
1774+
bucketAuto("price", 5)
1775+
1776+
// will generate {$bucketAuto: {groupBy: $price, buckets: 5, granularity: "E24"}}
1777+
bucketAuto("price", 5).withGranularity(Granularities.E24).withDefault("Other");
1778+
1779+
// will generate {$bucketAuto: {groupBy: $price, buckets: 5, output: { titles: { $push: "$title"}}}
1780+
bucketAuto("price", 5).andOutput("title").push().as("titles");
1781+
----
1782+
====
1783+
1784+
Bucket operations can use `AggregationExpression` via `andOutput()` and <<mongo.aggregation.projection.expressions, SpEL expressions>> via `andOutputExpression()` to create output fields in buckets.
1785+
1786+
Note that further details regarding bucket expressions can be found in the http://docs.mongodb.org/manual/reference/operator/aggregation/bucket/[`$bucket` section] and
1787+
http://docs.mongodb.org/manual/reference/operator/aggregation/bucketAuto/[`$bucketAuto` section] of the MongoDB Aggregation Framework reference documentation.
1788+
1789+
==== Multi-faceted aggregation
1790+
1791+
Multiple aggregation pipelines can be used to create multi-faceted aggregations which characterize data across multiple dimensions, or facets, within a single aggregation stage. Multi-faceted aggregations provide multiple filters and categorizations to guide data browsing and analysis. A common implementation of faceting is how many online retailers provide ways to narrow down search results by applying filters on product price, manufacturer, size, etc.
1792+
1793+
A `FacetOperation` can be defined via the `facet()` method of the `Aggregation` class. It can be customized with multiple aggregation pipelines via the `and()` method. Each sub-pipeline has its own field in the output document where its results are stored as an array of documents.
1794+
1795+
Sub-pipelines can project and filter input documents prior grouping. Common cases are extraction of date parts or calculations before categorization.
1796+
1797+
.Facet operation examples
1798+
====
1799+
[source,java]
1800+
----
1801+
// will generate {$facet: {categorizedByPrice: [ { $match: { price: {$exists : true}}}, { $bucketAuto: {groupBy: $price, buckets: 5}}]}}
1802+
facet(match(Criteria.where("price").exists(true)), bucketAuto("price", 5)).as("categorizedByPrice"))
1803+
1804+
// will generate {$facet: {categorizedByYear: [
1805+
// { $project: { title: 1, publicationYear: { $year: "publicationDate"}}},
1806+
// { $bucketAuto: {groupBy: $price, buckets: 5, output: { titles: {$push:"$title"}}}
1807+
// ]}}
1808+
facet(project("title").and("publicationDate").extractYear().as("publicationYear"),
1809+
bucketAuto("publicationYear", 5).andOutput("title").push().as("titles"))
1810+
.as("categorizedByYear"))
1811+
----
1812+
====
1813+
1814+
Note that further details regarding facet operation can be found in the http://docs.mongodb.org/manual/reference/operator/aggregation/facet/[`$facet` section] of the MongoDB Aggregation Framework reference documentation.
1815+
17381816
[[mongo.aggregation.projection.expressions]]
17391817
==== Spring Expression Support in Projection Expressions
17401818

1741-
As of Version 1.4.0 we support the use of SpEL expression in projection expressions via the `andExpression` method of the `ProjectionOperation` class. This allows you to define the desired expression as a SpEL expression which is translated into a corresponding MongoDB projection expression part on query execution. This makes it much easier to express complex calculations.
1819+
We support the use of SpEL expression in projection expressions via the `andExpression` method of the `ProjectionOperation` and `BucketOperation` classes. This allows you to define the desired expression as a SpEL expression which is translated into a corresponding MongoDB projection expression part on query execution. This makes it much easier to express complex calculations.
17421820

17431821
===== Complex calculations with SpEL expressions
17441822

0 commit comments

Comments
 (0)