Skip to content

Commit ef0893f

Browse files
authored
RUST-2002 Allow update to supply sort option (#1222)
1 parent 1f35649 commit ef0893f

17 files changed

+1851
-0
lines changed

Diff for: src/action/replace_one.rs

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ impl<'a> ReplaceOne<'a> {
7272
write_concern: WriteConcern,
7373
let_vars: Document,
7474
comment: Bson,
75+
sort: Document,
7576
}
7677

7778
/// Use the provided session when running the operation.

Diff for: src/action/update.rs

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ impl<'a> Update<'a> {
122122
write_concern: WriteConcern,
123123
let_vars: Document,
124124
comment: Bson,
125+
sort: Document,
125126
);
126127

127128
/// Use the provided session when running the operation.

Diff for: src/client/options/bulk_write.rs

+8
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ pub struct UpdateOneModel {
124124
///
125125
/// Defaults to false.
126126
pub upsert: Option<bool>,
127+
128+
/// Specify which document the operation updates if the query matches multiple
129+
/// documents. The first document matched by the sort order will be updated.
130+
pub sort: Option<Document>,
127131
}
128132

129133
impl From<UpdateOneModel> for WriteModel {
@@ -209,6 +213,10 @@ pub struct ReplaceOneModel {
209213
///
210214
/// Defaults to false.
211215
pub upsert: Option<bool>,
216+
217+
/// Specify which document the operation replaces if the query matches multiple
218+
/// documents. The first document matched by the sort order will be replaced.
219+
pub sort: Option<Document>,
212220
}
213221

214222
impl From<ReplaceOneModel> for WriteModel {

Diff for: src/coll/options.rs

+13
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ pub struct UpdateOptions {
239239
///
240240
/// This option is only available on server versions 4.4+.
241241
pub comment: Option<Bson>,
242+
243+
/// Specify which document the operation updates if the query matches multiple
244+
/// documents. The first document matched by the sort order will be updated.
245+
///
246+
/// Only available in MongoDB 8.0+.
247+
pub sort: Option<Document>,
242248
}
243249

244250
impl UpdateOptions {
@@ -251,6 +257,7 @@ impl UpdateOptions {
251257
collation: options.collation,
252258
let_vars: options.let_vars,
253259
comment: options.comment,
260+
sort: options.sort,
254261
..Default::default()
255262
}
256263
}
@@ -298,6 +305,12 @@ pub struct ReplaceOptions {
298305
///
299306
/// This option is only available on server versions 4.4+.
300307
pub comment: Option<Bson>,
308+
309+
/// Specify which document the operation replaces if the query matches multiple
310+
/// documents. The first document matched by the sort order will be replaced.
311+
///
312+
/// Only available in MongoDB 8.0+.
313+
pub sort: Option<Document>,
301314
}
302315

303316
/// Specifies the options to a

Diff for: src/operation/update.rs

+4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ impl OperationWithDefaults for Update {
140140
if let Some(ref comment) = options.comment {
141141
body.append("comment", RawBson::try_from(comment.clone())?);
142142
}
143+
144+
if let Some(ref sort) = options.sort {
145+
update.append("sort", RawDocumentBuf::from_document(sort)?);
146+
}
143147
};
144148

145149
if let Some(multi) = self.multi {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
{
2+
"description": "BulkWrite replaceOne-sort",
3+
"schemaVersion": "1.0",
4+
"createEntities": [
5+
{
6+
"client": {
7+
"id": "client0",
8+
"observeEvents": [
9+
"commandStartedEvent",
10+
"commandSucceededEvent"
11+
]
12+
}
13+
},
14+
{
15+
"database": {
16+
"id": "database0",
17+
"client": "client0",
18+
"databaseName": "crud-tests"
19+
}
20+
},
21+
{
22+
"collection": {
23+
"id": "collection0",
24+
"database": "database0",
25+
"collectionName": "coll0"
26+
}
27+
}
28+
],
29+
"initialData": [
30+
{
31+
"collectionName": "coll0",
32+
"databaseName": "crud-tests",
33+
"documents": [
34+
{
35+
"_id": 1,
36+
"x": 11
37+
},
38+
{
39+
"_id": 2,
40+
"x": 22
41+
},
42+
{
43+
"_id": 3,
44+
"x": 33
45+
}
46+
]
47+
}
48+
],
49+
"tests": [
50+
{
51+
"description": "BulkWrite replaceOne with sort option",
52+
"runOnRequirements": [
53+
{
54+
"minServerVersion": "8.0"
55+
}
56+
],
57+
"operations": [
58+
{
59+
"object": "collection0",
60+
"name": "bulkWrite",
61+
"arguments": {
62+
"requests": [
63+
{
64+
"replaceOne": {
65+
"filter": {
66+
"_id": {
67+
"$gt": 1
68+
}
69+
},
70+
"sort": {
71+
"_id": -1
72+
},
73+
"replacement": {
74+
"x": 1
75+
}
76+
}
77+
}
78+
]
79+
}
80+
}
81+
],
82+
"expectEvents": [
83+
{
84+
"client": "client0",
85+
"events": [
86+
{
87+
"commandStartedEvent": {
88+
"command": {
89+
"update": "coll0",
90+
"updates": [
91+
{
92+
"q": {
93+
"_id": {
94+
"$gt": 1
95+
}
96+
},
97+
"u": {
98+
"x": 1
99+
},
100+
"sort": {
101+
"_id": -1
102+
},
103+
"multi": {
104+
"$$unsetOrMatches": false
105+
},
106+
"upsert": {
107+
"$$unsetOrMatches": false
108+
}
109+
}
110+
]
111+
}
112+
}
113+
},
114+
{
115+
"commandSucceededEvent": {
116+
"reply": {
117+
"ok": 1,
118+
"n": 1
119+
},
120+
"commandName": "update"
121+
}
122+
}
123+
]
124+
}
125+
],
126+
"outcome": [
127+
{
128+
"collectionName": "coll0",
129+
"databaseName": "crud-tests",
130+
"documents": [
131+
{
132+
"_id": 1,
133+
"x": 11
134+
},
135+
{
136+
"_id": 2,
137+
"x": 22
138+
},
139+
{
140+
"_id": 3,
141+
"x": 1
142+
}
143+
]
144+
}
145+
]
146+
},
147+
{
148+
"description": "BulkWrite replaceOne with sort option unsupported (server-side error)",
149+
"runOnRequirements": [
150+
{
151+
"maxServerVersion": "7.99"
152+
}
153+
],
154+
"operations": [
155+
{
156+
"object": "collection0",
157+
"name": "bulkWrite",
158+
"arguments": {
159+
"requests": [
160+
{
161+
"replaceOne": {
162+
"filter": {
163+
"_id": {
164+
"$gt": 1
165+
}
166+
},
167+
"sort": {
168+
"_id": -1
169+
},
170+
"replacement": {
171+
"x": 1
172+
}
173+
}
174+
}
175+
]
176+
},
177+
"expectError": {
178+
"isClientError": false
179+
}
180+
}
181+
],
182+
"expectEvents": [
183+
{
184+
"client": "client0",
185+
"events": [
186+
{
187+
"commandStartedEvent": {
188+
"command": {
189+
"update": "coll0",
190+
"updates": [
191+
{
192+
"q": {
193+
"_id": {
194+
"$gt": 1
195+
}
196+
},
197+
"u": {
198+
"x": 1
199+
},
200+
"sort": {
201+
"_id": -1
202+
},
203+
"multi": {
204+
"$$unsetOrMatches": false
205+
},
206+
"upsert": {
207+
"$$unsetOrMatches": false
208+
}
209+
}
210+
]
211+
}
212+
}
213+
}
214+
]
215+
}
216+
],
217+
"outcome": [
218+
{
219+
"collectionName": "coll0",
220+
"databaseName": "crud-tests",
221+
"documents": [
222+
{
223+
"_id": 1,
224+
"x": 11
225+
},
226+
{
227+
"_id": 2,
228+
"x": 22
229+
},
230+
{
231+
"_id": 3,
232+
"x": 33
233+
}
234+
]
235+
}
236+
]
237+
}
238+
]
239+
}

0 commit comments

Comments
 (0)