Skip to content

Commit 48b9694

Browse files
MONGOID-???? Replace field[0] == ?$ with field.start_with?('$') -- easier to read and 30%+ faster (#5052)
Co-authored-by: shields <[email protected]>
1 parent e8aae88 commit 48b9694

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

lib/mongoid/criteria/queryable/selectable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def not(*criteria)
552552
end
553553
_mongoid_expand_keys(new_s).each do |k, v|
554554
k = k.to_s
555-
if c.selector[k] || k[0] == ?$
555+
if c.selector[k] || k.start_with?('$')
556556
c = c.send(:__multi__, [{'$nor' => [{k => v}]}], '$and')
557557
else
558558
if v.is_a?(Hash)
@@ -831,7 +831,7 @@ def expr_query(criterion)
831831
clone.tap do |query|
832832
normalized.each do |field, value|
833833
field_s = field.to_s
834-
if field_s[0] == ?$
834+
if field_s.start_with?('$')
835835
# Query expression-level operator, like $and or $where
836836
query.add_operator_expression(field_s, value)
837837
else

lib/mongoid/criteria/queryable/storable.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def add_field_expression(field, value)
3737
raise ArgumentError, "Field must be a string: #{field}"
3838
end
3939

40-
if field[0] == ?$
40+
if field.start_with?('$')
4141
raise ArgumentError, "Field cannot be an operator (i.e. begin with $): #{field}"
4242
end
4343

@@ -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[0] == ?$ && !selector[field].key?(key_s)
50+
key_s.start_with?('$') && !selector[field].key?(key_s)
5151
}
5252
then
5353
# Multiple operators can be combined on the same field by
@@ -184,7 +184,7 @@ def add_operator_expression(operator, op_expr)
184184
raise ArgumentError, "Operator must be a string: #{operator}"
185185
end
186186

187-
unless operator[0] == ?$
187+
unless operator.start_with?('$')
188188
raise ArgumentError, "Operator must begin with $: #{operator}"
189189
end
190190

@@ -219,7 +219,7 @@ def add_one_expression(field, value)
219219
raise ArgumentError, "Field must be a string: #{field}"
220220
end
221221

222-
if field[0] == ?$
222+
if field.start_with?('$')
223223
add_operator_expression(field, value)
224224
else
225225
add_field_expression(field, value)

0 commit comments

Comments
 (0)