Skip to content

Commit ff9355f

Browse files
authored
Update Updates.set to allow Bson value argument (#1493)
Improves the ergonomics of the Updates helper and keeps `$set` helpers inline with `$setOnInsert`. JAVA-5620
1 parent a5d636b commit ff9355f

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

driver-core/src/main/com/mongodb/client/model/Updates.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ public static Bson combine(final List<? extends Bson> updates) {
6666
return new CompositeUpdate(updates);
6767
}
6868

69+
/**
70+
* Creates an update that sets the values for the document.
71+
*
72+
* @param value the value
73+
* @return the update
74+
* @mongodb.driver.manual reference/operator/update/set/ $set
75+
*/
76+
public static Bson set(final Bson value) {
77+
return new SimpleBsonKeyValue("$set", value);
78+
}
79+
6980
/**
7081
* Creates an update that sets the value of the field with the given name to the given value.
7182
*

driver-core/src/test/unit/com/mongodb/client/model/UpdatesSpecification.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ class UpdatesSpecification extends Specification {
174174
}''')
175175
}
176176

177+
def 'should set document'() {
178+
toBson(set(parse('{ a : 1, b: "two"}'))) == parse('{$set : {a: 1, b: "two"} }')
179+
}
180+
177181
def 'should create string representation for simple updates'() {
178182
expect:
179183
set('x', 1).toString() == 'Update{fieldName=\'x\', operator=\'$set\', value=1}'

0 commit comments

Comments
 (0)