diff --git a/driver-core/src/main/com/mongodb/client/model/Accumulators.java b/driver-core/src/main/com/mongodb/client/model/Accumulators.java index 46300c5f319..149fc925528 100644 --- a/driver-core/src/main/com/mongodb/client/model/Accumulators.java +++ b/driver-core/src/main/com/mongodb/client/model/Accumulators.java @@ -148,6 +148,22 @@ public static BsonField addToSet(final String fieldName, final TEx return accumulatorOperator("$addToSet", fieldName, expression); } + + /** + * 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 document + * merged for the field. + * + * @param fieldName the field name + * @param expression the expression + * @param the expression type + * @return the field + * @mongodb.driver.manual reference/operator/aggregation/mergeObjects/ $mergeObjects + */ + public static BsonField mergeObjects(final String fieldName, final TExpression expression) { + return accumulatorOperator("$mergeObjects", fieldName, expression); + } + /** * Gets a field name for a $group operation representing the sample standard deviation of the values of the given expression * when applied to all members of the group. diff --git a/driver-core/src/test/functional/com/mongodb/client/model/AggregatesFunctionalSpecification.groovy b/driver-core/src/test/functional/com/mongodb/client/model/AggregatesFunctionalSpecification.groovy index 8e61f5d6a47..a2ebb8cbfef 100644 --- a/driver-core/src/test/functional/com/mongodb/client/model/AggregatesFunctionalSpecification.groovy +++ b/driver-core/src/test/functional/com/mongodb/client/model/AggregatesFunctionalSpecification.groovy @@ -37,6 +37,7 @@ import static com.mongodb.client.model.Accumulators.avg import static com.mongodb.client.model.Accumulators.first import static com.mongodb.client.model.Accumulators.last import static com.mongodb.client.model.Accumulators.max +import static com.mongodb.client.model.Accumulators.mergeObjects import static com.mongodb.client.model.Accumulators.min import static com.mongodb.client.model.Accumulators.push import static com.mongodb.client.model.Accumulators.stdDevPop @@ -91,16 +92,19 @@ class AggregatesFunctionalSpecification extends OperationFunctionalSpecification .append('z', false) .append('a', [1, 2, 3]) .append('a1', [new Document('c', 1).append('d', 2), new Document('c', 2).append('d', 3)]) + .append('o', new Document('a', 1)) def b = new Document('_id', 2).append('x', 2) .append('y', 'b') .append('z', true) .append('a', [3, 4, 5, 6]) .append('a1', [new Document('c', 2).append('d', 3), new Document('c', 3).append('d', 4)]) + .append('o', new Document('b', 2)) def c = new Document('_id', 3).append('x', 3) .append('y', 'c') .append('z', true) + .append('o', new Document('c', 3)) def setup() { getCollectionHelper().insertDocuments(a, b, c) @@ -203,6 +207,9 @@ class AggregatesFunctionalSpecification extends OperationFunctionalSpecification aggregate([group('$z', addToSet('acc', '$z'))]).containsAll([new Document('_id', true).append('acc', [true]), new Document('_id', false).append('acc', [false])]) + + aggregate([group(null, mergeObjects('acc', '$o'))]).containsAll( + [new Document('_id', null).append('acc',new Document('a', 1).append('b', 2).append('c', 3))]) } def '$out'() { diff --git a/driver-core/src/test/unit/com/mongodb/client/model/AggregatesSpecification.groovy b/driver-core/src/test/unit/com/mongodb/client/model/AggregatesSpecification.groovy index fcc25a1ab8f..fa15c80c9f9 100644 --- a/driver-core/src/test/unit/com/mongodb/client/model/AggregatesSpecification.groovy +++ b/driver-core/src/test/unit/com/mongodb/client/model/AggregatesSpecification.groovy @@ -33,6 +33,7 @@ import static com.mongodb.client.model.Accumulators.avg import static com.mongodb.client.model.Accumulators.first import static com.mongodb.client.model.Accumulators.last import static com.mongodb.client.model.Accumulators.max +import static com.mongodb.client.model.Accumulators.mergeObjects import static com.mongodb.client.model.Accumulators.min import static com.mongodb.client.model.Accumulators.push import static com.mongodb.client.model.Accumulators.stdDevPop @@ -439,6 +440,7 @@ class AggregatesSpecification extends Specification { first: { $first: "$quantity" }, last: { $last: "$quantity" }, all: { $push: "$quantity" }, + merged: { $mergeObjects: "$quantity" }, unique: { $addToSet: "$quantity" }, stdDevPop: { $stdDevPop: "$quantity" }, stdDevSamp: { $stdDevSamp: "$quantity" } @@ -452,6 +454,7 @@ class AggregatesSpecification extends Specification { first('first', '$quantity'), last('last', '$quantity'), push('all', '$quantity'), + mergeObjects('merged', '$quantity'), addToSet('unique', '$quantity'), stdDevPop('stdDevPop', '$quantity'), stdDevSamp('stdDevSamp', '$quantity') @@ -730,6 +733,7 @@ class AggregatesSpecification extends Specification { first('first', '$quantity'), last('last', '$quantity'), push('all', '$quantity'), + mergeObjects('merged', '$quantity'), addToSet('unique', '$quantity'), stdDevPop('stdDevPop', '$quantity'), stdDevSamp('stdDevSamp', '$quantity') @@ -741,6 +745,7 @@ class AggregatesSpecification extends Specification { first('first', '$quantity'), last('last', '$quantity'), push('all', '$quantity'), + mergeObjects('merged', '$quantity'), addToSet('unique', '$quantity'), stdDevPop('stdDevPop', '$quantity'), stdDevSamp('stdDevSamp', '$quantity') @@ -763,6 +768,7 @@ class AggregatesSpecification extends Specification { first('first', '$quantity'), last('last', '$quantity'), push('all', '$quantity'), + mergeObjects('merged', '$quantity'), addToSet('unique', '$quantity'), stdDevPop('stdDevPop', '$quantity'), stdDevSamp('stdDevSamp', '$quantity') @@ -775,6 +781,7 @@ class AggregatesSpecification extends Specification { first('first', '$quantity'), last('last', '$quantity'), push('all', '$quantity'), + mergeObjects('merged', '$quantity'), addToSet('unique', '$quantity'), stdDevPop('stdDevPop', '$quantity'), stdDevSamp('stdDevSamp', '$quantity')).hashCode()