diff --git a/docs/tutorials/ruby-driver-indexing.txt b/docs/tutorials/ruby-driver-indexing.txt index d187c35014..0a29b768cb 100644 --- a/docs/tutorials/ruby-driver-indexing.txt +++ b/docs/tutorials/ruby-driver-indexing.txt @@ -95,6 +95,9 @@ when creating indexes. These options mirror the options supported by the - The number of units within which to group the location values in a geo haystack index. * - ``:partial_filter_expression`` - A filter for a partial index. + * - ``:hidden`` + - A Boolean specifying whether the index should be hidden; a hidden index + is one that exists on the collection but will not be used by the query planner. The :commit_quorum option ------------------------- diff --git a/lib/mongo/index/view.rb b/lib/mongo/index/view.rb index 5774c253b5..5d8fa43588 100644 --- a/lib/mongo/index/view.rb +++ b/lib/mongo/index/view.rb @@ -120,6 +120,9 @@ def drop_all # a geo index. # @option options [ Hash ] :partial_filter_expression Specify a filter for a partial # index. + # @option options [ Boolean ] :hidden When :hidden is true, this index will + # exist on the collection but not be used by the query planner when + # executing operations. # @option options [ String | Integer ] :commit_quorum Specify how many # data-bearing members of a replica set, including the primary, must # complete the index builds successfully before the primary marks diff --git a/spec/mongo/index/view_spec.rb b/spec/mongo/index/view_spec.rb index a854f85e67..c68ce04705 100644 --- a/spec/mongo/index/view_spec.rb +++ b/spec/mongo/index/view_spec.rb @@ -325,6 +325,81 @@ end end + context 'when hidden is specified' do + let(:index) { view.get('with_hidden_1') } + + context 'on server versions <= 3.2' do + # DRIVERS-1220 Server versions 3.2 and older do not perform any option + # checking on index creation. The server will allow the user to create + # the index with the hidden option, but the server does not support this + # option and will not use it. + max_server_fcv '3.2' + + let!(:result) do + view.create_many({ key: { with_hidden: 1 }, hidden: true }) + end + + it 'returns ok' do + expect(result).to be_successful + end + + it 'creates an index' do + expect(index).to_not be_nil + end + end + + context 'on server versions between 3.4 and 4.2' do + max_server_fcv '4.2' + min_server_fcv '3.4' + + it 'raises an exception' do + expect do + view.create_many({ key: { with_hidden: 1 }, hidden: true }) + end.to raise_error(/The field 'hidden' is not valid for an index specification/) + end + end + + context 'on server versions >= 4.4' do + min_server_fcv '4.4' + + context 'when hidden is true' do + let!(:result) do + view.create_many({ key: { with_hidden: 1 }, hidden: true }) + end + + it 'returns ok' do + expect(result).to be_successful + end + + it 'creates an index' do + expect(index).to_not be_nil + end + + it 'applies the hidden option to the index' do + expect(index['hidden']).to be true + end + end + + context 'when hidden is false' do + let!(:result) do + view.create_many({ key: { with_hidden: 1 }, hidden: false }) + end + + it 'returns ok' do + expect(result).to be_successful + end + + it 'creates an index' do + expect(index).to_not be_nil + end + + it 'does not apply the hidden option to the index' do + expect(index['hidden']).to be_nil + end + end + end + end + context 'when collation is specified' do min_server_fcv '3.4' @@ -773,6 +848,77 @@ end end + context 'when providing hidden option' do + let(:index) { view.get('with_hidden_1') } + + context 'on server versions <= 3.2' do + # DRIVERS-1220 Server versions 3.2 and older do not perform any option + # checking on index creation. The server will allow the user to create + # the index with the hidden option, but the server does not support this + # option and will not use it. + max_server_fcv '3.2' + + let!(:result) do + view.create_one({ 'with_hidden' => 1 }, { hidden: true }) + end + + it 'returns ok' do + expect(result).to be_successful + end + + it 'creates an index' do + expect(index).to_not be_nil + end + end + + context 'on server versions between 3.4 and 4.2' do + max_server_fcv '4.2' + min_server_fcv '3.4' + + it 'raises an exception' do + expect do + view.create_one({ 'with_hidden' => 1 }, { hidden: true }) + end.to raise_error(/The field 'hidden' is not valid for an index specification/) + end + end + + context 'on server versions >= 4.4' do + min_server_fcv '4.4' + + context 'when hidden is true' do + let!(:result) { view.create_one({ 'with_hidden' => 1 }, { hidden: true }) } + + it 'returns ok' do + expect(result).to be_successful + end + + it 'creates an index' do + expect(index).to_not be_nil + end + + it 'applies the hidden option to the index' do + expect(index['hidden']).to be true + end + end + + context 'when hidden is false' do + let!(:result) { view.create_one({ 'with_hidden' => 1 }, { hidden: false }) } + + it 'returns ok' do + expect(result).to be_successful + end + + it 'creates an index' do + expect(index).to_not be_nil + end + + it 'does not apply the hidden option to the index' do + expect(index['hidden']).to be_nil + end + end + end + end + context 'when providing commit_quorum option' do require_topology :replica_set, :sharded context 'on server versions >= 4.4' do