Skip to content

Commit f634476

Browse files
neilshwekyp
authored andcommitted
RUBY-2295 include collation option in count_documents (#1984)
* RUBY-2295 include collation option in count_documents * RUBY-2295 clean up some code
1 parent 3d9a0dd commit f634476

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/mongo/collection/view/readable.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ def count_documents(opts = {})
185185
pipeline << { :'$limit' => opts[:limit] } if opts[:limit]
186186
pipeline << { :'$group' => { _id: 1, n: { :'$sum' => 1 } } }
187187

188-
opts.select! { |k, _| [:hint, :max_time_ms, :read, :collation, :session].include?(k) }
188+
opts = opts.select { |k, _| [:hint, :max_time_ms, :read, :collation, :session].include?(k) }
189+
opts[:collation] ||= collation
190+
189191
first = aggregate(pipeline, opts).first
190192
return 0 unless first
191193
first['n'].to_i

spec/mongo/collection_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,28 @@
945945
it_behaves_like 'a failed operation using a session'
946946
end
947947
end
948+
949+
context 'when collation has a strength' do
950+
let(:band_collection) do
951+
described_class.new(database, :bands)
952+
end
953+
954+
before do
955+
band_collection.delete_many
956+
band_collection.insert_many([{ name: "Depeche Mode" }, { name: "New Order" }])
957+
end
958+
959+
let(:options) do
960+
{ collation: { locale: 'en_US', strength: 2 } }
961+
end
962+
let(:band_result) do
963+
band_collection.find({ name: 'DEPECHE MODE' }, options)
964+
end
965+
966+
it 'finds Capitalize from UPPER CASE' do
967+
expect(band_result.count_documents).to eq(1)
968+
end
969+
end
948970
end
949971

950972
describe '#drop' do

0 commit comments

Comments
 (0)