Skip to content

RUST-2002 Allow update to supply sort option #1222

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 3 commits into from
Oct 11, 2024
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
1 change: 1 addition & 0 deletions src/action/replace_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl<'a> ReplaceOne<'a> {
write_concern: WriteConcern,
let_vars: Document,
comment: Bson,
sort: Document,
}

/// Use the provided session when running the operation.
Expand Down
1 change: 1 addition & 0 deletions src/action/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ impl<'a> Update<'a> {
write_concern: WriteConcern,
let_vars: Document,
comment: Bson,
sort: Document,
);

/// Use the provided session when running the operation.
Expand Down
8 changes: 8 additions & 0 deletions src/client/options/bulk_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ pub struct UpdateOneModel {
///
/// Defaults to false.
pub upsert: Option<bool>,

/// Specify which document the operation updates if the query matches multiple
/// documents. The first document matched by the sort order will be updated.
pub sort: Option<Document>,
}

impl From<UpdateOneModel> for WriteModel {
Expand Down Expand Up @@ -209,6 +213,10 @@ pub struct ReplaceOneModel {
///
/// Defaults to false.
pub upsert: Option<bool>,

/// Specify which document the operation replaces if the query matches multiple
/// documents. The first document matched by the sort order will be replaced.
pub sort: Option<Document>,
}

impl From<ReplaceOneModel> for WriteModel {
Expand Down
13 changes: 13 additions & 0 deletions src/coll/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ pub struct UpdateOptions {
///
/// This option is only available on server versions 4.4+.
pub comment: Option<Bson>,

/// Specify which document the operation updates if the query matches multiple
/// documents. The first document matched by the sort order will be updated.
///
/// Only available in MongoDB 8.0+.
pub sort: Option<Document>,
}

impl UpdateOptions {
Expand All @@ -251,6 +257,7 @@ impl UpdateOptions {
collation: options.collation,
let_vars: options.let_vars,
comment: options.comment,
sort: options.sort,
..Default::default()
}
}
Expand Down Expand Up @@ -298,6 +305,12 @@ pub struct ReplaceOptions {
///
/// This option is only available on server versions 4.4+.
pub comment: Option<Bson>,

/// Specify which document the operation replaces if the query matches multiple
/// documents. The first document matched by the sort order will be replaced.
///
/// Only available in MongoDB 8.0+.
pub sort: Option<Document>,
}

/// Specifies the options to a
Expand Down
4 changes: 4 additions & 0 deletions src/operation/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ impl OperationWithDefaults for Update {
if let Some(ref comment) = options.comment {
body.append("comment", RawBson::try_from(comment.clone())?);
}

if let Some(ref sort) = options.sort {
update.append("sort", RawDocumentBuf::from_document(sort)?);
}
};

if let Some(multi) = self.multi {
Expand Down
239 changes: 239 additions & 0 deletions src/test/spec/json/crud/unified/bulkWrite-replaceOne-sort.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
{
"description": "BulkWrite replaceOne-sort",
"schemaVersion": "1.0",
"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
}
]
}
],
"tests": [
{
"description": "BulkWrite replaceOne with sort option",
"runOnRequirements": [
{
"minServerVersion": "8.0"
}
],
"operations": [
{
"object": "collection0",
"name": "bulkWrite",
"arguments": {
"requests": [
{
"replaceOne": {
"filter": {
"_id": {
"$gt": 1
}
},
"sort": {
"_id": -1
},
"replacement": {
"x": 1
}
}
}
]
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"update": "coll0",
"updates": [
{
"q": {
"_id": {
"$gt": 1
}
},
"u": {
"x": 1
},
"sort": {
"_id": -1
},
"multi": {
"$$unsetOrMatches": false
},
"upsert": {
"$$unsetOrMatches": false
}
}
]
}
}
},
{
"commandSucceededEvent": {
"reply": {
"ok": 1,
"n": 1
},
"commandName": "update"
}
}
]
}
],
"outcome": [
{
"collectionName": "coll0",
"databaseName": "crud-tests",
"documents": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 1
}
]
}
]
},
{
"description": "BulkWrite replaceOne with sort option unsupported (server-side error)",
"runOnRequirements": [
{
"maxServerVersion": "7.99"
}
],
"operations": [
{
"object": "collection0",
"name": "bulkWrite",
"arguments": {
"requests": [
{
"replaceOne": {
"filter": {
"_id": {
"$gt": 1
}
},
"sort": {
"_id": -1
},
"replacement": {
"x": 1
}
}
}
]
},
"expectError": {
"isClientError": false
}
}
],
"expectEvents": [
{
"client": "client0",
"events": [
{
"commandStartedEvent": {
"command": {
"update": "coll0",
"updates": [
{
"q": {
"_id": {
"$gt": 1
}
},
"u": {
"x": 1
},
"sort": {
"_id": -1
},
"multi": {
"$$unsetOrMatches": false
},
"upsert": {
"$$unsetOrMatches": false
}
}
]
}
}
}
]
}
],
"outcome": [
{
"collectionName": "coll0",
"databaseName": "crud-tests",
"documents": [
{
"_id": 1,
"x": 11
},
{
"_id": 2,
"x": 22
},
{
"_id": 3,
"x": 33
}
]
}
]
}
]
}
Loading