Skip to content

Commit

Permalink
Mongoid::Criteria::Queryable::Storable#add_field_expression when valu…
Browse files Browse the repository at this point in the history
…e is a hash with symbol operator key add values with same operator keys using (#5660)
  • Loading branch information
JohnMaguir authored and comandeo-mongo committed Aug 8, 2023
1 parent 65d2d28 commit be61226
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mongoid/criteria/queryable/storable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def add_field_expression(field, value)
if value.is_a?(Hash) && selector[field].is_a?(Hash) &&
value.keys.all? { |key|
key_s = key.to_s
key_s.start_with?('$') && !selector[field].key?(key_s)
key_s.start_with?('$') && !selector[field].keys.map(&:to_s).include?(key_s)
}
then
# Multiple operators can be combined on the same field by
Expand Down
72 changes: 72 additions & 0 deletions spec/mongoid/criteria/queryable/storable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,79 @@
}
end
end

context 'when value is a hash combine values with different operator keys' do
let(:base) do
query.add_field_expression('foo', {'$in' => ['bar']})
end

let(:modified) do
base.add_field_expression('foo', {'$nin' => ['zoom']})
end

it 'combines the conditions using $and' do
modified.selector.should == {
'foo' => {
'$in' => ['bar'],
'$nin' => ['zoom']
}
}
end
end

context 'when value is a hash with symbol operator key combine values with different operator keys' do
let(:base) do
query.add_field_expression('foo', {:$in => ['bar']})
end

let(:modified) do
base.add_field_expression('foo', {:$nin => ['zoom']})
end

it 'combines the conditions using $and' do
modified.selector.should == {
'foo' => {
:$in => ['bar'],
:$nin => ['zoom']
}
}
end
end

context 'when value is a hash add values with same operator keys using $and' do
let(:base) do
query.add_field_expression('foo', {'$in' => ['bar']})
end

let(:modified) do
base.add_field_expression('foo', {'$in' => ['zoom']})
end

it 'adds the new condition using $and' do
modified.selector.should == {
'foo' => {'$in' => ['bar']},
'$and' => ['foo' => {'$in' => ['zoom']}]
}
end
end

context 'when value is a hash with symbol operator key add values with same operator keys using $and' do
let(:base) do
query.add_field_expression('foo', {:$in => ['bar']})
end

let(:modified) do
base.add_field_expression('foo', {:$in => ['zoom']})
end

it 'adds the new condition using $and' do
modified.selector.should == {
'foo' => {:$in => ['bar']},
'$and' => ['foo' => {:$in => ['zoom']}]
}
end
end
end

describe '#add_operator_expression' do
let(:query_method) { :add_operator_expression }
Expand Down

0 comments on commit be61226

Please sign in to comment.