Skip to content

Commit 1a3f710

Browse files
Clarify collation on collection
1 parent d9a5f1c commit 1a3f710

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

lib/mongo/collection.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@ def capped?
339339
# inserted or updated documents where the clustered index key value
340340
# matches an existing value in the index.
341341
# - *:name* -- Optional. A name that uniquely identifies the clustered index.
342-
# @option opts [ Hash ] :collation The collation to use.
342+
# @option opts [ Hash ] :collation The collation to use when creating the
343+
# collection. This option will not be sent to the server when calling
344+
# collection methods.
343345
# @option opts [ Hash ] :encrypted_fields Hash describing encrypted fields
344346
# for queryable encryption.
345347
# @option opts [ Integer ] :expire_after Number indicating

spec/integration/find_options_spec.rb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'Find operation options' do
6+
let(:subscriber) { Mrss::EventSubscriber.new }
7+
8+
let(:seeds) do
9+
[ SpecConfig.instance.addresses.first ]
10+
end
11+
12+
let(:client) do
13+
ClientRegistry.instance.new_local_client(
14+
seeds,
15+
SpecConfig.instance.test_options.merge(client_options)
16+
).tap do |client|
17+
client.subscribe(Mongo::Monitoring::COMMAND, subscriber)
18+
end
19+
end
20+
21+
let(:collection) do
22+
client['find_options', collection_options]
23+
end
24+
25+
let(:find_command) do
26+
subscriber.started_events.find { |cmd| cmd.command_name == 'find' }
27+
end
28+
29+
before do
30+
ClientRegistry.instance.global_client('authorized')['find_options'].drop
31+
collection.insert_many([ { a: 1 }, { a: 2 }, { a: 3 } ])
32+
end
33+
34+
describe 'collation' do
35+
let(:client_options) do
36+
{}
37+
end
38+
39+
let(:collation) do
40+
{ 'locale' => 'en_US' }
41+
end
42+
43+
context 'when defined on the collection' do
44+
let(:collection_options) do
45+
{ collation: collation }
46+
end
47+
48+
it 'uses the collation defined on the collection' do
49+
collection.find.to_a
50+
expect(find_command.command['collation']).to be_nil
51+
end
52+
end
53+
54+
context 'when defined on the operation' do
55+
let(:collection_options) do
56+
{}
57+
end
58+
59+
it 'uses the collation defined on the collection' do
60+
collection.find({}, collation: collation).to_a
61+
expect(find_command.command['collation']).to eq(collation)
62+
end
63+
end
64+
65+
context 'when defined on both collection and operation' do
66+
let(:collection_options) do
67+
{ 'locale' => 'de_AT' }
68+
end
69+
70+
it 'uses the collation defined on the collection' do
71+
collection.find({}, collation: collation).to_a
72+
expect(find_command.command['collation']).to eq(collation)
73+
end
74+
end
75+
end
76+
end

0 commit comments

Comments
 (0)