Skip to content

Commit 7123f84

Browse files
Jérome GUYONmp911de
authored andcommitted
DATAMONGO-1553 - Add $sortByCount aggregation stage.
Original pull request: #519.
1 parent 5cca849 commit 7123f84

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* @author Christoph Strobl
4848
* @author Nikolay Bogdanov
4949
* @author Gustavo de Geus
50+
* @author Jérôme Guyon
5051
* @since 1.3
5152
*/
5253
public class Aggregation {
@@ -480,6 +481,26 @@ public static BucketAutoOperation bucketAuto(AggregationExpression groupByExpres
480481
return new BucketAutoOperation(groupByExpression, buckets);
481482
}
482483

484+
/**
485+
* Creates a new {@link SortByCountOperation} given {@literal groupByField}
486+
*
487+
* @param groupByField must not be {@literal null} or empty.
488+
* @return
489+
*/
490+
public static SortByCountOperation sortByCount(String groupByField) {
491+
return new SortByCountOperation(field(groupByField));
492+
}
493+
494+
/**
495+
* Creates a new {@link SortByCountOperation} given {@link AggregationExpression group-by expression}.
496+
*
497+
* @param groupByExpression must not be {@literal null}.
498+
* @return
499+
*/
500+
public static SortByCountOperation sortByCount(AggregationExpression groupByExpression) {
501+
return new SortByCountOperation(groupByExpression);
502+
}
503+
483504
/**
484505
* Creates a new {@link FacetOperation}.
485506
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import org.bson.Document;
19+
import org.springframework.util.Assert;
20+
21+
/**
22+
* Encapsulates the aggregation framework {@code $sortByCount}-operation. <br />
23+
*
24+
* SortByCount stage is typically used with {@link Aggregation} and {@code $facet}. Groups incoming documents
25+
* based on the value of a specified expression, then computes the count of documents in each distinct group. <br />
26+
*
27+
* We recommend to use the static factory method {@link Aggregation#sortByCount(String)} instead of creating instances
28+
* of this class directly.
29+
*
30+
* @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/">https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/</a>
31+
* @author Jérôme GUYON
32+
*/
33+
public class SortByCountOperation implements AggregationOperation {
34+
35+
private final Field groupByField;
36+
private final AggregationExpression groupByExpression;
37+
38+
39+
/**
40+
* Creates a new {@link SortByCountOperation} given a {@link Field group-by field}.
41+
*
42+
* @param groupByField must not be {@literal null}.
43+
*/
44+
public SortByCountOperation(Field groupByField) {
45+
46+
Assert.notNull(groupByField, "Group by field must not be null!");
47+
48+
this.groupByField = groupByField;
49+
this.groupByExpression = null;
50+
}
51+
52+
/**
53+
* Creates a new {@link SortByCountOperation} given a {@link AggregationExpression group-by expression}.
54+
*
55+
* @param groupByExpression must not be {@literal null}.
56+
*/
57+
public SortByCountOperation(AggregationExpression groupByExpression) {
58+
59+
Assert.notNull(groupByExpression, "Group by AggregationExpression must not be null!");
60+
61+
this.groupByExpression = groupByExpression;
62+
this.groupByField = null;
63+
}
64+
65+
@Override
66+
public Document toDocument(AggregationOperationContext context) {
67+
return new Document("$sortByCount",
68+
groupByExpression == null ? context.getReference(groupByField).toString()
69+
: groupByExpression.toDocument(context)
70+
);
71+
}
72+
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/FacetOperationUnitTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Unit tests for {@link FacetOperation}.
2929
*
3030
* @author Mark Paluch
31+
* @author Jérôme Guyon
3132
* @soundtrack Stanley Foort - You Make Me Believe In Magic (Extended Mix)
3233
*/
3334
public class FacetOperationUnitTests {
@@ -101,4 +102,17 @@ public void shouldHonorProjectedFields() {
101102
+ "{ $bucketAuto: { buckets: 5, groupBy: \"$price\", "
102103
+ "output: { titles: { $push: \"$name\" } } } } ] } }")));
103104
}
105+
106+
@Test // DATAMONGO-1553
107+
public void shouldRenderSortByCountCorrectly() {
108+
109+
FacetOperation facetOperation = new FacetOperation()
110+
.and(sortByCount("country"))
111+
.as("categorizedByCountry");
112+
113+
Document agg = facetOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
114+
115+
assertThat(agg,
116+
is(Document.parse("{ $facet: { categorizedByCountry: [{ $sortByCount: \"$country\" } ] } }")));
117+
}
104118
}

src/main/asciidoc/reference/mongodb.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,19 @@ More examples for project operations can be found in the `AggregationTests` clas
19141914

19151915
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.
19161916

1917+
==== SortByCount
1918+
1919+
SortByCount operations groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group. SortVyCount operations require a grouping field or grouping expression. They can be defined via the `sortByCount()` methods of the `Aggregate` class.
1920+
1921+
.SortByCount operation example
1922+
====
1923+
[source,java]
1924+
----
1925+
// will generate { $sortByCount: "$country" }
1926+
sortByCount("country");
1927+
----
1928+
====
1929+
19171930
==== Buckets
19181931

19191932
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.
@@ -1975,6 +1988,9 @@ Sub-pipelines can project and filter input documents prior grouping. Common case
19751988
// will generate {$facet: {categorizedByPrice: [ { $match: { price: {$exists : true}}}, { $bucketAuto: {groupBy: $price, buckets: 5}}]}}
19761989
facet(match(Criteria.where("price").exists(true)), bucketAuto("price", 5)).as("categorizedByPrice"))
19771990
1991+
// will generate {$facet: {categorizedByCountry: [ { $match: { country: {$exists : true}}}, { $sortByCount: "$country"}]}}
1992+
facet(match(Criteria.where("country").exists(true)), sortByCount("country")).as("categorizedByCountry"))
1993+
19781994
// will generate {$facet: {categorizedByYear: [
19791995
// { $project: { title: 1, publicationYear: { $year: "publicationDate"}}},
19801996
// { $bucketAuto: {groupBy: $price, buckets: 5, output: { titles: {$push:"$title"}}}

0 commit comments

Comments
 (0)