Skip to content

Commit 3e2c7fd

Browse files
authored
Mongoid::Criteria::Queryable::Storable#add_field_expression when value is a hash with symbol operator key add values with same operator keys using (#5660)
1 parent 626a9ba commit 3e2c7fd

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

lib/mongoid/criteria/queryable/storable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def add_field_expression(field, value)
4848
if value.is_a?(Hash) && selector[field].is_a?(Hash) &&
4949
value.keys.all? { |key|
5050
key_s = key.to_s
51-
key_s.start_with?('$') && !selector[field].key?(key_s)
51+
key_s.start_with?('$') && !selector[field].keys.map(&:to_s).include?(key_s)
5252
}
5353
then
5454
# Multiple operators can be combined on the same field by

spec/mongoid/criteria/queryable/storable_spec.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,79 @@
188188
}
189189
end
190190
end
191+
192+
context 'when value is a hash combine values with different operator keys' do
193+
let(:base) do
194+
query.add_field_expression('foo', {'$in' => ['bar']})
195+
end
196+
197+
let(:modified) do
198+
base.add_field_expression('foo', {'$nin' => ['zoom']})
199+
end
200+
201+
it 'combines the conditions using $and' do
202+
modified.selector.should == {
203+
'foo' => {
204+
'$in' => ['bar'],
205+
'$nin' => ['zoom']
206+
}
207+
}
208+
end
209+
end
210+
211+
context 'when value is a hash with symbol operator key combine values with different operator keys' do
212+
let(:base) do
213+
query.add_field_expression('foo', {:$in => ['bar']})
214+
end
215+
216+
let(:modified) do
217+
base.add_field_expression('foo', {:$nin => ['zoom']})
218+
end
219+
220+
it 'combines the conditions using $and' do
221+
modified.selector.should == {
222+
'foo' => {
223+
:$in => ['bar'],
224+
:$nin => ['zoom']
225+
}
226+
}
227+
end
228+
end
229+
230+
context 'when value is a hash add values with same operator keys using $and' do
231+
let(:base) do
232+
query.add_field_expression('foo', {'$in' => ['bar']})
233+
end
234+
235+
let(:modified) do
236+
base.add_field_expression('foo', {'$in' => ['zoom']})
237+
end
238+
239+
it 'adds the new condition using $and' do
240+
modified.selector.should == {
241+
'foo' => {'$in' => ['bar']},
242+
'$and' => ['foo' => {'$in' => ['zoom']}]
243+
}
244+
end
245+
end
246+
247+
context 'when value is a hash with symbol operator key add values with same operator keys using $and' do
248+
let(:base) do
249+
query.add_field_expression('foo', {:$in => ['bar']})
250+
end
251+
252+
let(:modified) do
253+
base.add_field_expression('foo', {:$in => ['zoom']})
254+
end
255+
256+
it 'adds the new condition using $and' do
257+
modified.selector.should == {
258+
'foo' => {:$in => ['bar']},
259+
'$and' => ['foo' => {:$in => ['zoom']}]
260+
}
261+
end
191262
end
263+
end
192264

193265
describe '#add_operator_expression' do
194266
let(:query_method) { :add_operator_expression }

0 commit comments

Comments
 (0)