Skip to content

Client Bulk Write sort option. #1612

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

Merged
merged 4 commits into from
Feb 4, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,19 @@ static ClientReplaceOneOptions clientReplaceOneOptions() {
*/
@Override
ClientReplaceOneOptions upsert(@Nullable Boolean upsert);

/**
* Sets the sort criteria to apply to the operation. A null value means no sort criteria is set.
*
* <p>
* The sort criteria determines which document the operation replaces if the query matches multiple documents.
* The first document matched by the specified sort criteria will be replaced.
*
* @param sort The sort criteria. {@code null} represents the server default.
* @return this
* @mongodb.driver.manual reference/method/db.collection.replaceOne/ Sort
* @mongodb.server.release 8.0
* @since 5.4
*/
ClientReplaceOneOptions sort(@Nullable Bson sort);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,19 @@ static ClientUpdateOneOptions clientUpdateOneOptions() {
*/
@Override
ClientUpdateOneOptions upsert(@Nullable Boolean upsert);

/**
* Sets the sort criteria to apply to the operation. A null value means no sort criteria is set.
*
* <p>
* The sort criteria determines which document the operation updates if the query matches multiple documents.
* The first document matched by the specified sort criteria will be updated.
*
* @param sort The sort criteria. {@code null} represents the server default.
* @return this
* @mongodb.driver.manual reference/method/db.collection.updateOne/ Sort
* @mongodb.server.release 8.0
* @since 5.4
*/
ClientUpdateOneOptions sort(@Nullable Bson sort);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public final class ConcreteClientReplaceOneOptions implements ClientReplaceOneOp
private String hintString;
@Nullable
private Boolean upsert;
@Nullable
private Bson sort;

public ConcreteClientReplaceOneOptions() {
}
Expand Down Expand Up @@ -89,6 +91,21 @@ public ClientReplaceOneOptions upsert(@Nullable final Boolean upsert) {
return this;
}

/**
* @see ClientReplaceOneOptions#sort(Bson)
*/
public ClientReplaceOneOptions sort(final Bson sort) {
this.sort = sort;
return this;
}

/**
* @see ClientReplaceOneOptions#sort(Bson)
*/
public Optional<Bson> getSort() {
return ofNullable(sort);
}

/**
* @see #upsert(Boolean)
*/
Expand All @@ -103,6 +120,7 @@ public String toString() {
+ ", hint=" + hint
+ ", hintString='" + hintString + '\''
+ ", upsert=" + upsert
+ ", sort=" + sort
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@
import com.mongodb.lang.Nullable;
import org.bson.conversions.Bson;

import java.util.Optional;

import static java.util.Optional.ofNullable;

/**
* This class is not part of the public API and may be removed or changed at any time.
*/
public final class ConcreteClientUpdateOneOptions extends AbstractClientUpdateOptions implements ClientUpdateOneOptions {
static final ConcreteClientUpdateOneOptions MUTABLE_EMPTY = new ConcreteClientUpdateOneOptions();

@Nullable
private Bson sort;

public ConcreteClientUpdateOneOptions() {
}

Expand Down Expand Up @@ -54,6 +61,21 @@ public ConcreteClientUpdateOneOptions upsert(@Nullable final Boolean upsert) {
return (ConcreteClientUpdateOneOptions) super.upsert(upsert);
}

/**
* @see ClientUpdateOneOptions#sort(Bson)
*/
public ConcreteClientUpdateOneOptions sort(final Bson sort) {
this.sort = sort;
return this;
}

/**
* @see ClientUpdateOneOptions#sort(Bson)
*/
public Optional<Bson> getSort() {
return ofNullable(sort);
}

@Override
public String toString() {
return "ClientUpdateOneOptions{"
Expand All @@ -62,6 +84,7 @@ public String toString() {
+ ", hint=" + getHint().orElse(null)
+ ", hintString=" + getHintString().map(s -> '\'' + s + '\'') .orElse(null)
+ ", upsert=" + isUpsert().orElse(null)
+ ", sort=" + getSort().orElse(null)
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public ClusterableServer create(final Cluster cluster, final ServerAddress serve
new InternalStreamConnectionFactory(clusterMode, true, heartbeatStreamFactory, null, applicationName,
mongoDriverInformation, emptyList(), loggerSettings, null, serverApi),
clusterMode, serverApi, isFunctionAsAServiceEnvironment, sdamProvider, heartbeatOperationContextFactory);

ConnectionPool connectionPool = new DefaultConnectionPool(serverId,
new InternalStreamConnectionFactory(clusterMode, streamFactory, credential, applicationName,
mongoDriverInformation, compressorList, loggerSettings, commandListener, serverApi),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,14 @@ private void encodeWriteModelInternals(
});
}

private void encodeWriteModelInternals(final BsonWriter writer, final ConcreteClientUpdateOneModel model) {
encodeWriteModelInternals(writer, (AbstractClientUpdateModel<?>) model);
model.getOptions().getSort().ifPresent(value -> {
writer.writeName("sort");
encodeUsingRegistry(writer, value);
});
}

private void encodeWriteModelInternals(final BsonWriter writer, final AbstractClientUpdateModel<?> model) {
writer.writeName("filter");
encodeUsingRegistry(writer, model.getFilter());
Expand Down Expand Up @@ -1294,6 +1302,10 @@ private void encodeWriteModelInternals(final BsonBinaryWriter writer, final Conc
});
options.getHintString().ifPresent(value -> writer.writeString("hint", value));
options.isUpsert().ifPresent(value -> writer.writeBoolean("upsert", value));
options.getSort().ifPresent(value -> {
writer.writeName("sort");
encodeUsingRegistry(writer, value);
});
}

private void encodeWriteModelInternals(final BsonWriter writer, final AbstractClientDeleteModel<?> model) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"description": "client bulkWrite updateOne-sort",
"schemaVersion": "1.4",
"runOnRequirements": [
{
"minServerVersion": "8.0",
"serverless": "forbid"
}
],
"createEntities": [
{
"client": {
"id": "client0",
"observeEvents": [
"commandStartedEvent",
"commandSucceededEvent"
]
}
},
{
"database": {
"id": "database0",
"client": "client0",
"databaseName": "crud-tests"
}
},
{
"collection": {
"id": "collection0",
"database": "database0",
"collectionName": "coll0"
}
}
],
"initialData": [
{
"collectionName": "coll0",
"databaseName": "crud-tests",
"documents": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
],
"_yamlAnchors": {
"namespace": "crud-tests.coll0"
},
"tests": [
{
"description": "client bulkWrite replaceOne with sort option",
"operations": [
{
"object": "client0",
"name": "clientBulkWrite",
"arguments": {
"models": [
{
"replaceOne": {
"namespace": "crud-tests.coll0",
"filter": {
"_id": {
"$gt": 1
}
},
"sort": {
"_id": -1
},
"replacement": {
"x": 1
}
}
}
]
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"commandName": "bulkWrite",
"databaseName": "admin",
"command": {
"bulkWrite": 1,
"ops": [
{
"update": 0,
"filter": {
"_id": {
"$gt": 1
}
},
"updateMods": {
"x": 1
},
"sort": {
"_id": -1
},
"multi": {
"$$unsetOrMatches": false
},
"upsert": {
"$$unsetOrMatches": false
}
}
],
"nsInfo": [
{
"ns": "crud-tests.coll0"
}
]
}
}
},
{
"commandSucceededEvent": {
"reply": {
"ok": 1,
"nErrors": 0,
"nMatched": 1,
"nModified": 1
},
"commandName": "bulkWrite"
}
}
]
}
],
"outcome": [
{
"collectionName": "coll0",
"databaseName": "crud-tests",
"documents": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 1
}
]
}
]
}
]
}
Loading