Skip to content
Closed
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
16 changes: 16 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 @@ -148,6 +148,22 @@ public static <TExpression> 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 <TExpression> the expression type
* @return the field
* @mongodb.driver.manual reference/operator/aggregation/mergeObjects/ $mergeObjects
*/
public static <TExpression> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" }
Expand All @@ -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')
Expand Down Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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()
Expand Down