Skip to content

Commit 780138c

Browse files
RUBY-3520 Sort for updateOne and replaceOne
1 parent 6956636 commit 780138c

File tree

7 files changed

+389
-2
lines changed

7 files changed

+389
-2
lines changed

lib/mongo/bulk_write/transformable.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ module Transformable
9999
d['upsert'] = true if doc[:upsert]
100100
d[Operation::COLLATION] = doc[:collation] if doc[:collation]
101101
d['hint'] = doc[:hint] if doc[:hint]
102+
d['sort'] = doc[:sort] if doc[:sort]
102103
end
103104
}
104105

@@ -130,6 +131,7 @@ module Transformable
130131
d[Operation::COLLATION] = doc[:collation] if doc[:collation]
131132
d[Operation::ARRAY_FILTERS] = doc[:array_filters] if doc[:array_filters]
132133
d['hint'] = doc[:hint] if doc[:hint]
134+
d['sort'] = doc[:sort] if doc[:sort]
133135
end
134136
}
135137

lib/mongo/collection/view/writable.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ def replace_one(replacement, opts = {})
410410
Operation::U => replacement,
411411
hint: opts[:hint],
412412
collation: opts[:collation] || opts['collation'] || collation,
413+
sort: opts[:sort] || opts['sort'],
413414
}.compact
414415
if opts[:upsert]
415416
update_doc['upsert'] = true
@@ -570,6 +571,7 @@ def update_one(spec, opts = {})
570571
Operation::U => spec,
571572
hint: opts[:hint],
572573
collation: opts[:collation] || opts['collation'] || collation,
574+
sort: opts[:sort] || opts['sort'],
573575
}.compact
574576
if opts[:upsert]
575577
update_doc['upsert'] = true

spec/runners/unified/crud_operations.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ def update_one(op)
194194
hint: args.use('hint'),
195195
upsert: args.use('upsert'),
196196
timeout_ms: args.use('timeoutMS'),
197-
max_time_ms: args.use('maxTimeMS')
197+
max_time_ms: args.use('maxTimeMS'),
198+
sort: args.use('sort')
198199
}
199200
if session = args.use('session')
200201
opts[:session] = entities.get(:session, session)
@@ -228,7 +229,8 @@ def replace_one(op)
228229
let: args.use('let'),
229230
hint: args.use('hint'),
230231
timeout_ms: args.use('timeoutMS'),
231-
max_time_ms: args.use('maxTimeMS')
232+
max_time_ms: args.use('maxTimeMS'),
233+
sort: args.use('sort'),
232234
)
233235
end
234236
end
@@ -358,6 +360,9 @@ def convert_bulk_write_spec(spec)
358360
else
359361
raise NotImplementedError, "Unknown operation #{op}"
360362
end
363+
if %w[ updateOne replaceOne ].include?(op)
364+
out[:sort] = spec.use('sort') if spec.key?('sort')
365+
end
361366
unless spec.empty?
362367
raise NotImplementedError, "Unhandled keys: #{spec}"
363368
end
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
description: BulkWrite replaceOne-sort
2+
3+
schemaVersion: "1.0"
4+
5+
createEntities:
6+
- client:
7+
id: &client0 client0
8+
observeEvents: [ commandStartedEvent, commandSucceededEvent ]
9+
- database:
10+
id: &database0 database0
11+
client: *client0
12+
databaseName: &database0Name crud-tests
13+
- collection:
14+
id: &collection0 collection0
15+
database: *database0
16+
collectionName: &collection0Name coll0
17+
18+
initialData:
19+
- collectionName: *collection0Name
20+
databaseName: *database0Name
21+
documents:
22+
- { _id: 1, x: 11 }
23+
- { _id: 2, x: 22 }
24+
- { _id: 3, x: 33 }
25+
26+
tests:
27+
- description: BulkWrite replaceOne with sort option
28+
runOnRequirements:
29+
- minServerVersion: "8.0"
30+
operations:
31+
- object: *collection0
32+
name: bulkWrite
33+
arguments:
34+
requests:
35+
- replaceOne:
36+
filter: { _id: { $gt: 1 } }
37+
sort: { _id: -1 }
38+
replacement: { x: 1 }
39+
expectEvents:
40+
- client: *client0
41+
events:
42+
- commandStartedEvent:
43+
command:
44+
update: *collection0Name
45+
updates:
46+
- q: { _id: { $gt: 1 } }
47+
u: { x: 1 }
48+
sort: { _id: -1 }
49+
multi: { $$unsetOrMatches: false }
50+
upsert: { $$unsetOrMatches: false }
51+
- commandSucceededEvent:
52+
reply: { ok: 1, n: 1 }
53+
commandName: update
54+
outcome:
55+
- collectionName: *collection0Name
56+
databaseName: *database0Name
57+
documents:
58+
- { _id: 1, x: 11 }
59+
- { _id: 2, x: 22 }
60+
- { _id: 3, x: 1 }
61+
62+
- description: BulkWrite replaceOne with sort option unsupported (server-side error)
63+
runOnRequirements:
64+
- maxServerVersion: "7.99"
65+
operations:
66+
- object: *collection0
67+
name: bulkWrite
68+
arguments:
69+
requests:
70+
- replaceOne:
71+
filter: { _id: { $gt: 1 } }
72+
sort: { _id: -1 }
73+
replacement: { x: 1 }
74+
expectError:
75+
isClientError: false
76+
expectEvents:
77+
- client: *client0
78+
events:
79+
- commandStartedEvent:
80+
command:
81+
update: *collection0Name
82+
updates:
83+
- q: { _id: { $gt: 1 } }
84+
u: { x: 1 }
85+
sort: { _id: -1 }
86+
multi: { $$unsetOrMatches: false }
87+
upsert: { $$unsetOrMatches: false }
88+
outcome:
89+
- collectionName: *collection0Name
90+
databaseName: *database0Name
91+
documents:
92+
- { _id: 1, x: 11 }
93+
- { _id: 2, x: 22 }
94+
- { _id: 3, x: 33 }
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
description: BulkWrite updateOne-sort
2+
3+
schemaVersion: "1.0"
4+
5+
createEntities:
6+
- client:
7+
id: &client0 client0
8+
observeEvents: [ commandStartedEvent, commandSucceededEvent ]
9+
- database:
10+
id: &database0 database0
11+
client: *client0
12+
databaseName: &database0Name crud-tests
13+
- collection:
14+
id: &collection0 collection0
15+
database: *database0
16+
collectionName: &collection0Name coll0
17+
18+
initialData:
19+
- collectionName: *collection0Name
20+
databaseName: *database0Name
21+
documents:
22+
- { _id: 1, x: 11 }
23+
- { _id: 2, x: 22 }
24+
- { _id: 3, x: 33 }
25+
26+
tests:
27+
- description: BulkWrite updateOne with sort option
28+
runOnRequirements:
29+
- minServerVersion: "8.0"
30+
operations:
31+
- object: *collection0
32+
name: bulkWrite
33+
arguments:
34+
requests:
35+
- updateOne:
36+
filter: { _id: { $gt: 1 } }
37+
sort: { _id: -1 }
38+
update: [ $set: { x: 1 } ]
39+
expectEvents:
40+
- client: *client0
41+
events:
42+
- commandStartedEvent:
43+
command:
44+
update: *collection0Name
45+
updates:
46+
- q: { _id: { $gt: 1 } }
47+
u: [ $set: { x: 1 } ]
48+
sort: { _id: -1 }
49+
multi: { $$unsetOrMatches: false }
50+
upsert: { $$unsetOrMatches: false }
51+
- commandSucceededEvent:
52+
reply: { ok: 1, n: 1 }
53+
commandName: update
54+
outcome:
55+
- collectionName: *collection0Name
56+
databaseName: *database0Name
57+
documents:
58+
- { _id: 1, x: 11 }
59+
- { _id: 2, x: 22 }
60+
- { _id: 3, x: 1 }
61+
62+
- description: BulkWrite updateOne with sort option unsupported (server-side error)
63+
runOnRequirements:
64+
- maxServerVersion: "7.99"
65+
operations:
66+
- object: *collection0
67+
name: bulkWrite
68+
arguments:
69+
requests:
70+
- updateOne:
71+
filter: { _id: { $gt: 1 } }
72+
sort: { _id: -1 }
73+
update: [ $set: { x: 1 } ]
74+
expectError:
75+
isClientError: false
76+
expectEvents:
77+
- client: *client0
78+
events:
79+
- commandStartedEvent:
80+
command:
81+
update: *collection0Name
82+
updates:
83+
- q: { _id: { $gt: 1 } }
84+
u: [ $set: { x: 1 } ]
85+
sort: { _id: -1 }
86+
multi: { $$unsetOrMatches: false }
87+
upsert: { $$unsetOrMatches: false }
88+
outcome:
89+
- collectionName: *collection0Name
90+
databaseName: *database0Name
91+
documents:
92+
- { _id: 1, x: 11 }
93+
- { _id: 2, x: 22 }
94+
- { _id: 3, x: 33 }
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
description: replaceOne-sort
2+
3+
schemaVersion: "1.0"
4+
5+
createEntities:
6+
- client:
7+
id: &client0 client0
8+
observeEvents: [ commandStartedEvent, commandSucceededEvent ]
9+
- database:
10+
id: &database0 database0
11+
client: *client0
12+
databaseName: &database0Name crud-tests
13+
- collection:
14+
id: &collection0 collection0
15+
database: *database0
16+
collectionName: &collection0Name coll0
17+
18+
initialData:
19+
- collectionName: *collection0Name
20+
databaseName: *database0Name
21+
documents:
22+
- { _id: 1, x: 11 }
23+
- { _id: 2, x: 22 }
24+
- { _id: 3, x: 33 }
25+
26+
tests:
27+
- description: ReplaceOne with sort option
28+
runOnRequirements:
29+
- minServerVersion: "8.0"
30+
operations:
31+
- name: replaceOne
32+
object: *collection0
33+
arguments:
34+
filter: { _id: { $gt: 1 } }
35+
sort: { _id: -1 }
36+
replacement: { x: 1 }
37+
expectResult:
38+
matchedCount: 1
39+
modifiedCount: 1
40+
upsertedCount: 0
41+
expectEvents:
42+
- client: *client0
43+
events:
44+
- commandStartedEvent:
45+
command:
46+
update: *collection0Name
47+
updates:
48+
- q: { _id: { $gt: 1 } }
49+
u: { x: 1 }
50+
sort: { _id: -1 }
51+
multi: { $$unsetOrMatches: false }
52+
upsert: { $$unsetOrMatches: false }
53+
- commandSucceededEvent:
54+
reply: { ok: 1, n: 1 }
55+
commandName: update
56+
outcome:
57+
- collectionName: *collection0Name
58+
databaseName: *database0Name
59+
documents:
60+
- { _id: 1, x: 11 }
61+
- { _id: 2, x: 22 }
62+
- { _id: 3, x: 1 }
63+
64+
- description: replaceOne with sort option unsupported (server-side error)
65+
runOnRequirements:
66+
- maxServerVersion: "7.99"
67+
operations:
68+
- name: replaceOne
69+
object: *collection0
70+
arguments:
71+
filter: { _id: { $gt: 1 } }
72+
sort: { _id: -1 }
73+
replacement: { x: 1 }
74+
expectError:
75+
isClientError: false
76+
expectEvents:
77+
- client: *client0
78+
events:
79+
- commandStartedEvent:
80+
command:
81+
update: *collection0Name
82+
updates:
83+
- q: { _id: { $gt: 1 } }
84+
u: { x: 1 }
85+
sort: { _id: -1 }
86+
multi: { $$unsetOrMatches: false }
87+
upsert: { $$unsetOrMatches: false }
88+
outcome:
89+
- collectionName: *collection0Name
90+
databaseName: *database0Name
91+
documents:
92+
- { _id: 1, x: 11 }
93+
- { _id: 2, x: 22 }
94+
- { _id: 3, x: 33 }

0 commit comments

Comments
 (0)