Skip to content

Commit 20a56ce

Browse files
author
Emily Giurleo
committed
RUBY-2243 Support hidden option on index creation (#2030)
1 parent 1e2a4d3 commit 20a56ce

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

docs/tutorials/ruby-driver-indexing.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ when creating indexes. These options mirror the options supported by the
9595
- The number of units within which to group the location values in a geo haystack index.
9696
* - ``:partial_filter_expression``
9797
- A filter for a partial index.
98+
* - ``:hidden``
99+
- A Boolean specifying whether the index should be hidden; a hidden index
100+
is one that exists on the collection but will not be used by the query planner.
98101

99102
The :commit_quorum option
100103
-------------------------

lib/mongo/index/view.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ def drop_all
120120
# a geo index.
121121
# @option options [ Hash ] :partial_filter_expression Specify a filter for a partial
122122
# index.
123+
# @option options [ Boolean ] :hidden When :hidden is true, this index will
124+
# exist on the collection but not be used by the query planner when
125+
# executing operations.
123126
# @option options [ String | Integer ] :commit_quorum Specify how many
124127
# data-bearing members of a replica set, including the primary, must
125128
# complete the index builds successfully before the primary marks

spec/mongo/index/view_spec.rb

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,81 @@
325325
end
326326
end
327327

328+
context 'when hidden is specified' do
329+
let(:index) { view.get('with_hidden_1') }
330+
331+
context 'on server versions <= 3.2' do
332+
# DRIVERS-1220 Server versions 3.2 and older do not perform any option
333+
# checking on index creation. The server will allow the user to create
334+
# the index with the hidden option, but the server does not support this
335+
# option and will not use it.
336+
max_server_fcv '3.2'
337+
338+
let!(:result) do
339+
view.create_many({ key: { with_hidden: 1 }, hidden: true })
340+
end
341+
342+
it 'returns ok' do
343+
expect(result).to be_successful
344+
end
345+
346+
it 'creates an index' do
347+
expect(index).to_not be_nil
348+
end
349+
end
350+
351+
context 'on server versions between 3.4 and 4.2' do
352+
max_server_fcv '4.2'
353+
min_server_fcv '3.4'
354+
355+
it 'raises an exception' do
356+
expect do
357+
view.create_many({ key: { with_hidden: 1 }, hidden: true })
358+
end.to raise_error(/The field 'hidden' is not valid for an index specification/)
359+
end
360+
end
361+
362+
context 'on server versions >= 4.4' do
363+
min_server_fcv '4.4'
364+
365+
context 'when hidden is true' do
366+
let!(:result) do
367+
view.create_many({ key: { with_hidden: 1 }, hidden: true })
368+
end
369+
370+
it 'returns ok' do
371+
expect(result).to be_successful
372+
end
373+
374+
it 'creates an index' do
375+
expect(index).to_not be_nil
376+
end
377+
378+
it 'applies the hidden option to the index' do
379+
expect(index['hidden']).to be true
380+
end
381+
end
382+
383+
context 'when hidden is false' do
384+
let!(:result) do
385+
view.create_many({ key: { with_hidden: 1 }, hidden: false })
386+
end
387+
388+
it 'returns ok' do
389+
expect(result).to be_successful
390+
end
391+
392+
it 'creates an index' do
393+
expect(index).to_not be_nil
394+
end
395+
396+
it 'does not apply the hidden option to the index' do
397+
expect(index['hidden']).to be_nil
398+
end
399+
end
400+
end
401+
end
402+
328403
context 'when collation is specified' do
329404
min_server_fcv '3.4'
330405

@@ -773,6 +848,77 @@
773848
end
774849
end
775850

851+
context 'when providing hidden option' do
852+
let(:index) { view.get('with_hidden_1') }
853+
854+
context 'on server versions <= 3.2' do
855+
# DRIVERS-1220 Server versions 3.2 and older do not perform any option
856+
# checking on index creation. The server will allow the user to create
857+
# the index with the hidden option, but the server does not support this
858+
# option and will not use it.
859+
max_server_fcv '3.2'
860+
861+
let!(:result) do
862+
view.create_one({ 'with_hidden' => 1 }, { hidden: true })
863+
end
864+
865+
it 'returns ok' do
866+
expect(result).to be_successful
867+
end
868+
869+
it 'creates an index' do
870+
expect(index).to_not be_nil
871+
end
872+
end
873+
874+
context 'on server versions between 3.4 and 4.2' do
875+
max_server_fcv '4.2'
876+
min_server_fcv '3.4'
877+
878+
it 'raises an exception' do
879+
expect do
880+
view.create_one({ 'with_hidden' => 1 }, { hidden: true })
881+
end.to raise_error(/The field 'hidden' is not valid for an index specification/)
882+
end
883+
end
884+
885+
context 'on server versions >= 4.4' do
886+
min_server_fcv '4.4'
887+
888+
context 'when hidden is true' do
889+
let!(:result) { view.create_one({ 'with_hidden' => 1 }, { hidden: true }) }
890+
891+
it 'returns ok' do
892+
expect(result).to be_successful
893+
end
894+
895+
it 'creates an index' do
896+
expect(index).to_not be_nil
897+
end
898+
899+
it 'applies the hidden option to the index' do
900+
expect(index['hidden']).to be true
901+
end
902+
end
903+
904+
context 'when hidden is false' do
905+
let!(:result) { view.create_one({ 'with_hidden' => 1 }, { hidden: false }) }
906+
907+
it 'returns ok' do
908+
expect(result).to be_successful
909+
end
910+
911+
it 'creates an index' do
912+
expect(index).to_not be_nil
913+
end
914+
915+
it 'does not apply the hidden option to the index' do
916+
expect(index['hidden']).to be_nil
917+
end
918+
end
919+
end
920+
end
921+
776922
context 'when providing commit_quorum option' do
777923
require_topology :replica_set, :sharded
778924
context 'on server versions >= 4.4' do

0 commit comments

Comments
 (0)