Skip to content

Commit 2b6544f

Browse files
JohnMaguircomandeo-mongo
authored andcommitted
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 8af6ba9 commit 2b6544f

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
@@ -47,7 +47,7 @@ def add_field_expression(field, value)
4747
if value.is_a?(Hash) && selector[field].is_a?(Hash) &&
4848
value.keys.all? { |key|
4949
key_s = key.to_s
50-
key_s.start_with?('$') && !selector[field].key?(key_s)
50+
key_s.start_with?('$') && !selector[field].keys.map(&:to_s).include?(key_s)
5151
}
5252
then
5353
# 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
@@ -210,7 +210,79 @@
210210
}
211211
end
212212
end
213+
214+
context 'when value is a hash combine values with different operator keys' do
215+
let(:base) do
216+
query.add_field_expression('foo', {'$in' => ['bar']})
217+
end
218+
219+
let(:modified) do
220+
base.add_field_expression('foo', {'$nin' => ['zoom']})
221+
end
222+
223+
it 'combines the conditions using $and' do
224+
modified.selector.should == {
225+
'foo' => {
226+
'$in' => ['bar'],
227+
'$nin' => ['zoom']
228+
}
229+
}
230+
end
231+
end
232+
233+
context 'when value is a hash with symbol operator key combine values with different operator keys' do
234+
let(:base) do
235+
query.add_field_expression('foo', {:$in => ['bar']})
236+
end
237+
238+
let(:modified) do
239+
base.add_field_expression('foo', {:$nin => ['zoom']})
240+
end
241+
242+
it 'combines the conditions using $and' do
243+
modified.selector.should == {
244+
'foo' => {
245+
:$in => ['bar'],
246+
:$nin => ['zoom']
247+
}
248+
}
249+
end
250+
end
251+
252+
context 'when value is a hash add values with same operator keys using $and' do
253+
let(:base) do
254+
query.add_field_expression('foo', {'$in' => ['bar']})
255+
end
256+
257+
let(:modified) do
258+
base.add_field_expression('foo', {'$in' => ['zoom']})
259+
end
260+
261+
it 'adds the new condition using $and' do
262+
modified.selector.should == {
263+
'foo' => {'$in' => ['bar']},
264+
'$and' => ['foo' => {'$in' => ['zoom']}]
265+
}
266+
end
267+
end
268+
269+
context 'when value is a hash with symbol operator key add values with same operator keys using $and' do
270+
let(:base) do
271+
query.add_field_expression('foo', {:$in => ['bar']})
272+
end
273+
274+
let(:modified) do
275+
base.add_field_expression('foo', {:$in => ['zoom']})
276+
end
277+
278+
it 'adds the new condition using $and' do
279+
modified.selector.should == {
280+
'foo' => {:$in => ['bar']},
281+
'$and' => ['foo' => {:$in => ['zoom']}]
282+
}
283+
end
213284
end
285+
end
214286

215287
describe '#add_operator_expression' do
216288
let(:query_method) { :add_operator_expression }

0 commit comments

Comments
 (0)