Skip to content

JAVA-5829: Add $concatArrays and $setUnion accumulators #1680

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Pull Requests
Pull requests should generally be made against the master (default) branch and include relevant tests, if applicable.

Code should compile with the Java 9 compiler and tests should pass under all Java versions which the driver currently
supports. Currently the Java driver supports a minimum version of Java 8. Please run './gradlew test' to confirm. By default, running the
supports. Currently the Java driver supports a minimum version of Java 8. Please run `./gradlew test` to confirm. By default, running the
tests requires that you start a mongod server on localhost, listening on the default port and configured to run with
[`enableTestCommands`](https://www.mongodb.com/docs/manual/reference/parameters/#param.enableTestCommands), which may be set with the
`--setParameter enableTestCommands=1` command-line parameter. At minimum, please test against the latest release version of the MongoDB
Expand Down
29 changes: 29 additions & 0 deletions driver-core/src/main/com/mongodb/client/model/Accumulators.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,35 @@ public static <TExpression> BsonField addToSet(final String fieldName, final TEx
return accumulatorOperator("$addToSet", fieldName, expression);
}

/**
* Gets a field name for a $group operation that concatenates arrays from the given expressions into a single array.
*
* @param fieldName the field name for the concatenated array
* @param expressions the list of array expressions to concatenate
* @param <TExpression> the expression type
* @return the field
* @mongodb.driver.manual reference/operator/aggregation/concatArrays/ $concatArrays
* @mongodb.server.release 8.1
* @since 5.4
*/
public static <TExpression> BsonField concatArrays(final String fieldName, final List<TExpression> expressions) {
return accumulatorOperator("$concatArrays", fieldName, expressions);
}

/**
* Gets a field name for a $group operation that computes the union of arrays from the given expressions, removing duplicates.
*
* @param fieldName the field name for the union array
* @param expressions the list of array expressions to union
* @param <TExpression> the expression type
* @return the field
* @mongodb.driver.manual reference/operator/aggregation/setUnion/ $setUnion
* @mongodb.server.release 8.1
* @since 5.4
*/
public static <TExpression> BsonField setUnion(final String fieldName, final List<TExpression> expressions) {
return accumulatorOperator("$setUnion", fieldName, expressions);
}

/**
* Gets a field name for a $group operation representing the result of merging the fields of the documents.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,34 @@ public object Accumulators {
public fun <TExpression> addToSet(property: KProperty<*>, expression: TExpression): BsonField =
Accumulators.addToSet(property.path(), expression)

/**
* Gets a field name for a $group operation that concatenates arrays from the given expressions into a single array.
*
* @param property The data class property computed by the accumulator
* @param expressions The list of array expressions to concatenate
* @param <TExpression> The expression type
* @return The field
* @mongodb.driver.manual reference/operator/aggregation/concatArrays/ $concatArrays
* @mongodb.server.release 8.1
* @since 5.4
*/
public fun <TExpression> concatArrays(property: KProperty<*>, expressions: List<TExpression>): BsonField =
Accumulators.concatArrays(property.path(), expressions)

/**
* Gets a field name for a $group operation that computes the union of arrays from the given expressions, removing duplicates.
*
* @param property The data class property computed by the accumulator
* @param expressions The list of array expressions to union
* @param <TExpression> The expression type
* @return The field
* @mongodb.driver.manual reference/operator/aggregation/setUnion/ $setUnion
* @mongodb.server.release 8.1
* @since 5.4
*/
public fun <TExpression> setUnion(property: KProperty<*>, expressions: List<TExpression>): BsonField =
Accumulators.setUnion(property.path(), expressions)

/**
* Gets a field name for a $group operation representing the result of merging the fields of the documents. If
* documents to merge include the same field name, the field, in the resulting document, has the value from the last
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import com.mongodb.client.model.densify.DensifyRange
import com.mongodb.kotlin.client.MongoCollection
import com.mongodb.kotlin.client.model.Accumulators.accumulator
import com.mongodb.kotlin.client.model.Accumulators.addToSet
import com.mongodb.kotlin.client.model.Accumulators.concatArrays
import com.mongodb.kotlin.client.model.Accumulators.setUnion
import com.mongodb.kotlin.client.model.Accumulators.avg
import com.mongodb.kotlin.client.model.Accumulators.bottom
import com.mongodb.kotlin.client.model.Accumulators.bottomN
Expand Down Expand Up @@ -284,6 +286,16 @@ class AggregatesTest {

assertEquals(com.mongodb.client.model.Accumulators.addToSet("age", 1), addToSet(Person::age, 1))

assertEquals(
com.mongodb.client.model.Accumulators.concatArrays("results", listOf("\$array1", "\$array2")),
concatArrays(Person::results, listOf("\$array1", "\$array2"))
)

assertEquals(
com.mongodb.client.model.Accumulators.setUnion("results", listOf("\$expression1", "\$expression2")),
setUnion(Person::results, listOf("\$expression1", "\$expression2"))
)

assertEquals(com.mongodb.client.model.Accumulators.mergeObjects("age", 1), mergeObjects(Person::age, 1))

assertEquals(
Expand Down