Skip to content

Commit d3e81d8

Browse files
authoredFeb 4, 2025
Client Bulk Write sort option. (#1612)
- Add sort option to updateOne. - Add sort option to replaceOne. JAVA-5723 --------- Co-authored-by: Maxim Katcharov <maxim.katcharov@mongodb.com>
1 parent 0e25654 commit d3e81d8

File tree

9 files changed

+431
-4
lines changed

9 files changed

+431
-4
lines changed
 

‎driver-core/src/main/com/mongodb/client/model/bulk/ClientReplaceOneOptions.java

+15
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,19 @@ static ClientReplaceOneOptions clientReplaceOneOptions() {
7474
*/
7575
@Override
7676
ClientReplaceOneOptions upsert(@Nullable Boolean upsert);
77+
78+
/**
79+
* Sets the sort criteria to apply to the operation. A null value means no sort criteria is set.
80+
*
81+
* <p>
82+
* The sort criteria determines which document the operation replaces if the query matches multiple documents.
83+
* The first document matched by the specified sort criteria will be replaced.
84+
*
85+
* @param sort The sort criteria. {@code null} represents the server default.
86+
* @return this
87+
* @mongodb.driver.manual reference/method/db.collection.replaceOne/ Sort
88+
* @mongodb.server.release 8.0
89+
* @since 5.4
90+
*/
91+
ClientReplaceOneOptions sort(@Nullable Bson sort);
7792
}

‎driver-core/src/main/com/mongodb/client/model/bulk/ClientUpdateOneOptions.java

+15
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,19 @@ static ClientUpdateOneOptions clientUpdateOneOptions() {
8585
*/
8686
@Override
8787
ClientUpdateOneOptions upsert(@Nullable Boolean upsert);
88+
89+
/**
90+
* Sets the sort criteria to apply to the operation. A null value means no sort criteria is set.
91+
*
92+
* <p>
93+
* The sort criteria determines which document the operation updates if the query matches multiple documents.
94+
* The first document matched by the specified sort criteria will be updated.
95+
*
96+
* @param sort The sort criteria. {@code null} represents the server default.
97+
* @return this
98+
* @mongodb.driver.manual reference/method/db.collection.updateOne/ Sort
99+
* @mongodb.server.release 8.0
100+
* @since 5.4
101+
*/
102+
ClientUpdateOneOptions sort(@Nullable Bson sort);
88103
}

‎driver-core/src/main/com/mongodb/internal/client/model/bulk/ConcreteClientReplaceOneOptions.java

+18
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public final class ConcreteClientReplaceOneOptions implements ClientReplaceOneOp
3838
private String hintString;
3939
@Nullable
4040
private Boolean upsert;
41+
@Nullable
42+
private Bson sort;
4143

4244
public ConcreteClientReplaceOneOptions() {
4345
}
@@ -89,6 +91,21 @@ public ClientReplaceOneOptions upsert(@Nullable final Boolean upsert) {
8991
return this;
9092
}
9193

94+
/**
95+
* @see ClientReplaceOneOptions#sort(Bson)
96+
*/
97+
public ClientReplaceOneOptions sort(final Bson sort) {
98+
this.sort = sort;
99+
return this;
100+
}
101+
102+
/**
103+
* @see ClientReplaceOneOptions#sort(Bson)
104+
*/
105+
public Optional<Bson> getSort() {
106+
return ofNullable(sort);
107+
}
108+
92109
/**
93110
* @see #upsert(Boolean)
94111
*/
@@ -103,6 +120,7 @@ public String toString() {
103120
+ ", hint=" + hint
104121
+ ", hintString='" + hintString + '\''
105122
+ ", upsert=" + upsert
123+
+ ", sort=" + sort
106124
+ '}';
107125
}
108126
}

‎driver-core/src/main/com/mongodb/internal/client/model/bulk/ConcreteClientUpdateOneOptions.java

+23
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@
2020
import com.mongodb.lang.Nullable;
2121
import org.bson.conversions.Bson;
2222

23+
import java.util.Optional;
24+
25+
import static java.util.Optional.ofNullable;
26+
2327
/**
2428
* This class is not part of the public API and may be removed or changed at any time.
2529
*/
2630
public final class ConcreteClientUpdateOneOptions extends AbstractClientUpdateOptions implements ClientUpdateOneOptions {
2731
static final ConcreteClientUpdateOneOptions MUTABLE_EMPTY = new ConcreteClientUpdateOneOptions();
2832

33+
@Nullable
34+
private Bson sort;
35+
2936
public ConcreteClientUpdateOneOptions() {
3037
}
3138

@@ -54,6 +61,21 @@ public ConcreteClientUpdateOneOptions upsert(@Nullable final Boolean upsert) {
5461
return (ConcreteClientUpdateOneOptions) super.upsert(upsert);
5562
}
5663

64+
/**
65+
* @see ClientUpdateOneOptions#sort(Bson)
66+
*/
67+
public ConcreteClientUpdateOneOptions sort(final Bson sort) {
68+
this.sort = sort;
69+
return this;
70+
}
71+
72+
/**
73+
* @see ClientUpdateOneOptions#sort(Bson)
74+
*/
75+
public Optional<Bson> getSort() {
76+
return ofNullable(sort);
77+
}
78+
5779
@Override
5880
public String toString() {
5981
return "ClientUpdateOneOptions{"
@@ -62,6 +84,7 @@ public String toString() {
6284
+ ", hint=" + getHint().orElse(null)
6385
+ ", hintString=" + getHintString().map(s -> '\'' + s + '\'') .orElse(null)
6486
+ ", upsert=" + isUpsert().orElse(null)
87+
+ ", sort=" + getSort().orElse(null)
6588
+ '}';
6689
}
6790
}

‎driver-core/src/main/com/mongodb/internal/connection/DefaultClusterableServerFactory.java

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public ClusterableServer create(final Cluster cluster, final ServerAddress serve
9393
new InternalStreamConnectionFactory(clusterMode, true, heartbeatStreamFactory, null, applicationName,
9494
mongoDriverInformation, emptyList(), loggerSettings, null, serverApi),
9595
clusterMode, serverApi, isFunctionAsAServiceEnvironment, sdamProvider, heartbeatOperationContextFactory);
96+
9697
ConnectionPool connectionPool = new DefaultConnectionPool(serverId,
9798
new InternalStreamConnectionFactory(clusterMode, streamFactory, credential, applicationName,
9899
mongoDriverInformation, compressorList, loggerSettings, commandListener, serverApi),

‎driver-core/src/main/com/mongodb/internal/operation/ClientBulkWriteOperation.java

+12
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,14 @@ private void encodeWriteModelInternals(
12471247
});
12481248
}
12491249

1250+
private void encodeWriteModelInternals(final BsonWriter writer, final ConcreteClientUpdateOneModel model) {
1251+
encodeWriteModelInternals(writer, (AbstractClientUpdateModel<?>) model);
1252+
model.getOptions().getSort().ifPresent(value -> {
1253+
writer.writeName("sort");
1254+
encodeUsingRegistry(writer, value);
1255+
});
1256+
}
1257+
12501258
private void encodeWriteModelInternals(final BsonWriter writer, final AbstractClientUpdateModel<?> model) {
12511259
writer.writeName("filter");
12521260
encodeUsingRegistry(writer, model.getFilter());
@@ -1294,6 +1302,10 @@ private void encodeWriteModelInternals(final BsonBinaryWriter writer, final Conc
12941302
});
12951303
options.getHintString().ifPresent(value -> writer.writeString("hint", value));
12961304
options.isUpsert().ifPresent(value -> writer.writeBoolean("upsert", value));
1305+
options.getSort().ifPresent(value -> {
1306+
writer.writeName("sort");
1307+
encodeUsingRegistry(writer, value);
1308+
});
12971309
}
12981310

12991311
private void encodeWriteModelInternals(final BsonWriter writer, final AbstractClientDeleteModel<?> model) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
{
2+
"description": "client bulkWrite updateOne-sort",
3+
"schemaVersion": "1.4",
4+
"runOnRequirements": [
5+
{
6+
"minServerVersion": "8.0",
7+
"serverless": "forbid"
8+
}
9+
],
10+
"createEntities": [
11+
{
12+
"client": {
13+
"id": "client0",
14+
"observeEvents": [
15+
"commandStartedEvent",
16+
"commandSucceededEvent"
17+
]
18+
}
19+
},
20+
{
21+
"database": {
22+
"id": "database0",
23+
"client": "client0",
24+
"databaseName": "crud-tests"
25+
}
26+
},
27+
{
28+
"collection": {
29+
"id": "collection0",
30+
"database": "database0",
31+
"collectionName": "coll0"
32+
}
33+
}
34+
],
35+
"initialData": [
36+
{
37+
"collectionName": "coll0",
38+
"databaseName": "crud-tests",
39+
"documents": [
40+
{
41+
"_id": 1,
42+
"x": 11
43+
},
44+
{
45+
"_id": 2,
46+
"x": 22
47+
},
48+
{
49+
"_id": 3,
50+
"x": 33
51+
}
52+
]
53+
}
54+
],
55+
"_yamlAnchors": {
56+
"namespace": "crud-tests.coll0"
57+
},
58+
"tests": [
59+
{
60+
"description": "client bulkWrite replaceOne with sort option",
61+
"operations": [
62+
{
63+
"object": "client0",
64+
"name": "clientBulkWrite",
65+
"arguments": {
66+
"models": [
67+
{
68+
"replaceOne": {
69+
"namespace": "crud-tests.coll0",
70+
"filter": {
71+
"_id": {
72+
"$gt": 1
73+
}
74+
},
75+
"sort": {
76+
"_id": -1
77+
},
78+
"replacement": {
79+
"x": 1
80+
}
81+
}
82+
}
83+
]
84+
}
85+
}
86+
],
87+
"expectEvents": [
88+
{
89+
"client": "client0",
90+
"events": [
91+
{
92+
"commandStartedEvent": {
93+
"commandName": "bulkWrite",
94+
"databaseName": "admin",
95+
"command": {
96+
"bulkWrite": 1,
97+
"ops": [
98+
{
99+
"update": 0,
100+
"filter": {
101+
"_id": {
102+
"$gt": 1
103+
}
104+
},
105+
"updateMods": {
106+
"x": 1
107+
},
108+
"sort": {
109+
"_id": -1
110+
},
111+
"multi": {
112+
"$$unsetOrMatches": false
113+
},
114+
"upsert": {
115+
"$$unsetOrMatches": false
116+
}
117+
}
118+
],
119+
"nsInfo": [
120+
{
121+
"ns": "crud-tests.coll0"
122+
}
123+
]
124+
}
125+
}
126+
},
127+
{
128+
"commandSucceededEvent": {
129+
"reply": {
130+
"ok": 1,
131+
"nErrors": 0,
132+
"nMatched": 1,
133+
"nModified": 1
134+
},
135+
"commandName": "bulkWrite"
136+
}
137+
}
138+
]
139+
}
140+
],
141+
"outcome": [
142+
{
143+
"collectionName": "coll0",
144+
"databaseName": "crud-tests",
145+
"documents": [
146+
{
147+
"_id": 1,
148+
"x": 11
149+
},
150+
{
151+
"_id": 2,
152+
"x": 22
153+
},
154+
{
155+
"_id": 3,
156+
"x": 1
157+
}
158+
]
159+
}
160+
]
161+
}
162+
]
163+
}

0 commit comments

Comments
 (0)
Failed to load comments.