Module: Mongoid::Criteria::Queryable::Selectable

Extended by:
Macroable
Defined in:
build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb

Overview

An queryable selectable is selectable, in that it has the ability to select document from the database. The selectable module brings all functionality to the selectable that has to do with building MongoDB selectors.

Constant Summary collapse

LINE_STRING =

Constant for a LineString $geometry.

Since:

  • 2.0.0

"LineString"
POINT =

Constant for a Point $geometry.

Since:

  • 2.0.0

"Point"
POLYGON =

Constant for a Polygon $geometry.

Since:

  • 2.0.0

"Polygon"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Macroable

key

Instance Attribute Details

#negatingObject

Returns the value of attribute negating.



31
32
33
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 31

def negating
  @negating
end

#negating If the next expression is negated.(Ifthe) ⇒ Object



31
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 31

attr_accessor :negating, :selector

#selectorObject

Returns the value of attribute selector.



31
32
33
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 31

def selector
  @selector
end

#selector The query selector.(Thequeryselector.) ⇒ Object



31
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 31

attr_accessor :negating, :selector

Class Method Details

.forwardablesArray<Symbol>

Get the methods on the selectable that can be forwarded to from a model.

Examples:

Get the forwardable methods.

Selectable.forwardables

Returns:

  • (Array<Symbol>)

    The names of the forwardable methods.

Since:

  • 1.0.0



967
968
969
970
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 967

def forwardables
  public_instance_methods(false) -
    [ :negating, :negating=, :negating?, :selector, :selector= ]
end

Instance Method Details

#all(*criteria) ⇒ Selectable Also known as: all_in

Add the $all criterion.

Examples:

Add the criterion.

selectable.all(field: [ 1, 2 ])

Execute an $all in a where query.

selectable.where(:field.all => [ 1, 2 ])

Parameters:

  • criterion (Hash)

    The key value pairs for $all matching.

Returns:

Since:

  • 1.0.0



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 46

def all(*criteria)
  if criteria.empty?
    return clone.tap do |query|
      query.reset_strategies!
    end
  end

  criteria.inject(clone) do |query, condition|
    if condition.nil?
      raise Errors::CriteriaArgumentRequired, :all
    end

    condition = expand_condition_to_array_values(condition)

    if strategy
      send(strategy, condition, "$all")
    else
      condition.inject(query) do |_query, (field, value)|
        v = {'$all' => value}
        if negating?
          v = {'$not' => v}
        end
        _query.add_field_expression(field.to_s, v)
      end
    end
  end.reset_strategies!
end

#and(*criteria) ⇒ Selectable Also known as: all_of

Add the $and criterion.

Examples:

Add the criterion.

selectable.and({ field: value }, { other: value })

Parameters:

  • criteria (Array<Hash | Criteria>)

    Multiple key/value pair matches or Criteria objects that all must match to return results.

Returns:

Since:

  • 1.0.0



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 87

def and(*criteria)
  _mongoid_flatten_arrays(criteria).inject(self.clone) do |c, new_s|
    if new_s.is_a?(Selectable)
      new_s = new_s.selector
    end
    normalized = _mongoid_expand_keys(new_s)
    normalized.each do |k, v|
      k = k.to_s
      if c.selector[k]
        # There is already a condition on k.
        # If v is an operator, and all existing conditions are
        # also operators, and v isn't present in existing conditions,
        # we can add to existing conditions.
        # Otherwise use $and.
        if v.is_a?(Hash) &&
          v.length == 1 &&
          (new_k = v.keys.first).start_with?('$') &&
          (existing_kv = c.selector[k]).is_a?(Hash) &&
          !existing_kv.key?(new_k) &&
          existing_kv.keys.all? { |sub_k| sub_k.start_with?('$') }
        then
          merged_v = c.selector[k].merge(v)
          c.selector.store(k, merged_v)
        else
          c = c.send(:__multi__, [k => v], '$and')
        end
      else
        c.selector.store(k, v)
      end
    end
    c
  end
end

#any_of(*criteria) ⇒ Selectable

Adds a disjunction of the arguments as an additional constraint to the criteria already existing in the receiver.

Use or to make the receiver one of the disjunction operands.

Each argument can be a Hash, a Criteria object, an array of Hash or Criteria objects, or a nested array. Nested arrays will be flattened and can be of any depth. Passing arrays is deprecated.

Examples:

Add the $or selection where both fields must have the specified values.

selectable.any_of(field: 1, field: 2)

Add the $or selection where either value match is sufficient.

selectable.any_of({field: 1}, {field: 2})

Same as previous example but using the deprecated array wrap.

selectable.any_of([{field: 1}, {field: 2}])

Same as previous example, also deprecated.

selectable.any_of([{field: 1}], [{field: 2}])

Parameters:

  • criteria (Hash | Criteria | Array<Hash | Criteria>, ...)

    Multiple key/value pair matches or Criteria objects, or arrays thereof. Passing arrays is deprecated.

Returns:

Since:

  • 1.0.0



673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 673

def any_of(*criteria)
  criteria = _mongoid_flatten_arrays(criteria)
  case criteria.length
  when 0
    clone
  when 1
    # When we have a single criteria, any_of behaves like and.
    # Note: criteria can be a Query object, which #where method does
    # not support.
    self.and(*criteria)
  else
    # When we have multiple criteria, combine them all with $or
    # and add the result to self.
    exprs = criteria.map do |criterion|
      if criterion.is_a?(Selectable)
        _mongoid_expand_keys(criterion.selector)
      else
        Hash[criterion.map do |k, v|
          if k.is_a?(Symbol)
            [k.to_s, v]
          else
            [k, v]
          end
        end]
      end
    end
    # Should be able to do:
    #where('$or' => exprs)
    # But since that is broken do instead:
    clone.tap do |query|
      if query.selector['$or']
        query.selector.store('$or', query.selector['$or'] + exprs)
      else
        query.selector.store('$or', exprs)
      end
    end
  end
end

#between(criterion) ⇒ Selectable

Add the range selection.

Examples:

Match on results within a single range.

selectable.between(field: 1..2)

Match on results between multiple ranges.

selectable.between(field: 1..2, other: 5..7)

Parameters:

  • criterion (Hash)

    Multiple key/range pairs.

Returns:

Since:

  • 1.0.0



135
136
137
138
139
140
141
142
143
144
145
146
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 135

def between(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :between
  end

  selection(criterion) do |selector, field, value|
    selector.store(
      field,
      { "$gte" => value.min, "$lte" => value.max }
    )
  end
end

#elem_match(criterion) ⇒ Selectable

Select with an $elemMatch.

Examples:

Add criterion for a single match.

selectable.elem_match(field: { name: "value" })

Add criterion for multiple matches.

selectable.elem_match(
  field: { name: "value" },
  other: { name: "value"}
)

Execute an $elemMatch in a where query.

selectable.where(:field.elem_match => { name: "value" })

Parameters:

  • criterion (Hash)

    The field/match pairs.

Returns:

Since:

  • 1.0.0



167
168
169
170
171
172
173
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 167

def elem_match(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :elem_match
  end

  __override__(criterion, "$elemMatch")
end

#exists(criterion) ⇒ Selectable

Add the $exists selection.

Examples:

Add a single selection.

selectable.exists(field: true)

Add multiple selections.

selectable.exists(field: true, other: false)

Execute an $exists in a where query.

selectable.where(:field.exists => true)

Parameters:

  • criterion (Hash)

    The field/boolean existence checks.

Returns:

Since:

  • 1.0.0



192
193
194
195
196
197
198
199
200
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 192

def exists(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :exists
  end

  typed_override(criterion, "$exists") do |value|
    ::Boolean.evolve(value)
  end
end

#geo_spacial(criterion) ⇒ Selectable

Note:

The only valid geometry shapes for a $geoIntersects are: :intersects_line, :intersects_point, and :intersects_polygon.

Note:

The only valid options for a $geoWithin query are the geometry shape :within_polygon and the operator :within_box.

Note:

The :within_box operator for the $geoWithin query expects the lower left (south west) coordinate pair as the first argument and the upper right (north east) as the second argument. Important: When latitude and longitude are passed, longitude is expected as the first element of the coordinate pair. Source: docs.mongodb.com/manual/reference/operator/query/box/

Add a $geoIntersects or $geoWithin selection. Symbol operators must be used as shown in the examples to expand the criteria.

Examples:

Add a geo intersect criterion for a line.

query.geo_spacial(:location.intersects_line => [[ 1, 10 ], [ 2, 10 ]])

Add a geo intersect criterion for a point.

query.geo_spacial(:location.intersects_point => [[ 1, 10 ]])

Add a geo intersect criterion for a polygon.

query.geo_spacial(:location.intersects_polygon => [[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]])

Add a geo within criterion for a polygon.

query.geo_spacial(:location.within_polygon => [[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]])

Add a geo within criterion for a box.

query.geo_spacial(:location.within_box => [[ 1, 10 ], [ 2, 10 ])

Parameters:

  • criterion (Hash)

    The criterion.

Returns:

Since:

  • 2.0.0



241
242
243
244
245
246
247
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 241

def geo_spacial(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :geo_spacial
  end

  __merge__(criterion)
end

#gt(criterion) ⇒ Selectable

Add the $gt criterion to the selector.

Examples:

Add the $gt criterion.

selectable.gt(age: 60)

Execute an $gt in a where query.

selectable.where(:field.gt => 10)

Parameters:

  • criterion (Hash)

    The field/value pairs to check.

Returns:

Since:

  • 1.0.0



275
276
277
278
279
280
281
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 275

def gt(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :gt
  end

  __override__(criterion, "$gt")
end

#gte(criterion) ⇒ Selectable

Add the $gte criterion to the selector.

Examples:

Add the $gte criterion.

selectable.gte(age: 60)

Execute an $gte in a where query.

selectable.where(:field.gte => 10)

Parameters:

  • criterion (Hash)

    The field/value pairs to check.

Returns:

Since:

  • 1.0.0



297
298
299
300
301
302
303
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 297

def gte(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :gte
  end

  __override__(criterion, "$gte")
end

#in(condition) ⇒ Selectable Also known as: any_in

Adds the $in selection to the selectable.

Examples:

Add $in selection on an array.

selectable.in(age: [ 1, 2, 3 ])

Add $in selection on a range.

selectable.in(age: 18..24)

Execute an $in in a where query.

selectable.where(:field.in => [ 1, 2, 3 ])

Parameters:

  • condition (Hash)

    The field/value criterion pairs.

Returns:

Since:

  • 1.0.0



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 322

def in(condition)
  if condition.nil?
    raise Errors::CriteriaArgumentRequired, :in
  end

  condition = expand_condition_to_array_values(condition)

  if strategy
    send(strategy, condition, "$in")
  else
    condition.inject(clone) do |query, (field, value)|
      v = {'$in' => value}
      if negating?
        v = {'$not' => v}
      end
      query.add_field_expression(field.to_s, v)
    end.reset_strategies!
  end
end

#lt(criterion) ⇒ Selectable

Add the $lt criterion to the selector.

Examples:

Add the $lt criterion.

selectable.lt(age: 60)

Execute an $lt in a where query.

selectable.where(:field.lt => 10)

Parameters:

  • criterion (Hash)

    The field/value pairs to check.

Returns:

Since:

  • 1.0.0



357
358
359
360
361
362
363
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 357

def lt(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :lt
  end

  __override__(criterion, "$lt")
end

#lte(criterion) ⇒ Selectable

Add the $lte criterion to the selector.

Examples:

Add the $lte criterion.

selectable.lte(age: 60)

Execute an $lte in a where query.

selectable.where(:field.lte => 10)

Parameters:

  • criterion (Hash)

    The field/value pairs to check.

Returns:

Since:

  • 1.0.0



379
380
381
382
383
384
385
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 379

def lte(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :lte
  end

  __override__(criterion, "$lte")
end

#max_distance(criterion) ⇒ Selectable

Add a $maxDistance selection to the selectable.

Examples:

Add the $maxDistance selection.

selectable.max_distance(location: 10)

Parameters:

  • criterion (Hash)

    The field/distance pairs.

Returns:

Since:

  • 1.0.0



398
399
400
401
402
403
404
405
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 398

def max_distance(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :max_distance
  end

  # $maxDistance must be given together with $near
  __add__(criterion, "$maxDistance")
end

#mod(criterion) ⇒ Selectable

Adds $mod selection to the selectable.

Examples:

Add the $mod selection.

selectable.mod(field: [ 10, 1 ])

Execute an $mod in a where query.

selectable.where(:field.mod => [ 10, 1 ])

Parameters:

  • criterion (Hash)

    The field/mod selections.

Returns:

Since:

  • 1.0.0



420
421
422
423
424
425
426
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 420

def mod(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :mod
  end

  __override__(criterion, "$mod")
end

#ne(criterion) ⇒ Selectable Also known as: excludes

Adds $ne selection to the selectable.

Examples:

Query for a value $ne to something.

selectable.ne(field: 10)

Execute an $ne in a where query.

selectable.where(:field.ne => "value")

Parameters:

  • criterion (Hash)

    The field/ne selections.

Returns:

Since:

  • 1.0.0



442
443
444
445
446
447
448
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 442

def ne(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :ne
  end

  __override__(criterion, "$ne")
end

#near(criterion) ⇒ Selectable

Adds a $near criterion to a geo selection.

Examples:

Add the $near selection.

selectable.near(location: [ 23.1, 12.1 ])

Execute an $near in a where query.

selectable.where(:field.near => [ 23.2, 12.1 ])

Parameters:

  • criterion (Hash)

    The field/location pair.

Returns:

Since:

  • 1.0.0



465
466
467
468
469
470
471
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 465

def near(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :near
  end

  __override__(criterion, "$near")
end

#near_sphere(criterion) ⇒ Selectable

Adds a $nearSphere criterion to a geo selection.

Examples:

Add the $nearSphere selection.

selectable.near_sphere(location: [ 23.1, 12.1 ])

Execute an $nearSphere in a where query.

selectable.where(:field.near_sphere => [ 10.11, 3.22 ])

Parameters:

  • criterion (Hash)

    The field/location pair.

Returns:

Since:

  • 1.0.0



487
488
489
490
491
492
493
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 487

def near_sphere(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :near_sphere
  end

  __override__(criterion, "$nearSphere")
end

#negating?true, false

Is the current selectable negating the next selection?

Examples:

Is the selectable negating?

selectable.negating?

Returns:

  • (true, false)

    If the selectable is negating.

Since:

  • 1.0.0



557
558
559
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 557

def negating?
  !!negating
end

#nin(condition) ⇒ Selectable Also known as: not_in

Adds the $nin selection to the selectable.

Examples:

Add $nin selection on an array.

selectable.nin(age: [ 1, 2, 3 ])

Add $nin selection on a range.

selectable.nin(age: 18..24)

Execute an $nin in a where query.

selectable.where(:field.nin => [ 1, 2, 3 ])

Parameters:

  • condition (Hash)

    The field/value criterion pairs.

Returns:

Since:

  • 1.0.0



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 512

def nin(condition)
  if condition.nil?
    raise Errors::CriteriaArgumentRequired, :nin
  end

  condition = expand_condition_to_array_values(condition)

  if strategy
    send(strategy, condition, "$nin")
  else
    condition.inject(clone) do |query, (field, value)|
      v = {'$nin' => value}
      if negating?
        v = {'$not' => v}
      end
      query.add_field_expression(field.to_s, v)
    end.reset_strategies!
  end
end

#nor(*criteria) ⇒ Selectable

Adds $nor selection to the selectable.

Examples:

Add the $nor selection.

selectable.nor(field: 1, field: 2)

Parameters:

  • criteria (Array<Hash | Criteria>)

    Multiple key/value pair matches or Criteria objects.

Returns:

Since:

  • 1.0.0



545
546
547
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 545

def nor(*criteria)
  _mongoid_add_top_level_operation('$nor', criteria)
end

#not(*criteria) ⇒ Selectable

Negate the arguments, or the next selection if no arguments are given.

Examples:

Negate the next selection.

selectable.not.in(field: [ 1, 2 ])

Add the $not criterion.

selectable.not(name: /Bob/)

Execute a $not in a where query.

selectable.where(:field.not => /Bob/)

Parameters:

  • criteria (Array<Hash | Criteria>)

    Multiple key/value pair matches or Criteria objects to negate.

Returns:

Since:

  • 1.0.0



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 578

def not(*criteria)
  if criteria.empty?
    dup.tap { |query| query.negating = true }
  else
    criteria.compact.inject(self.clone) do |c, new_s|
      if new_s.is_a?(Selectable)
        new_s = new_s.selector
      end
      _mongoid_expand_keys(new_s).each do |k, v|
        k = k.to_s
        if c.selector[k] || k.start_with?('$')
          c = c.send(:__multi__, [{'$nor' => [{k => v}]}], '$and')
        else
          if v.is_a?(Hash)
            c = c.send(:__multi__, [{'$nor' => [{k => v}]}], '$and')
          else
            if v.is_a?(Regexp)
              negated_operator = '$not'
            else
              negated_operator = '$ne'
            end
            c = c.send(:__override__, {k => v}, negated_operator)
          end
        end
      end
      c
    end
  end
end

#or(*criteria) ⇒ Selectable

Creates a disjunction using $or from the existing criteria in the receiver and the provided arguments.

This behavior (receiver becoming one of the disjunction operands) matches ActiveRecord’s or behavior.

Use any_of to add a disjunction of the arguments as an additional constraint to the criteria already existing in the receiver.

Each argument can be a Hash, a Criteria object, an array of Hash or Criteria objects, or a nested array. Nested arrays will be flattened and can be of any depth. Passing arrays is deprecated.

Examples:

Add the $or selection where both fields must have the specified values.

selectable.or(field: 1, field: 2)

Add the $or selection where either value match is sufficient.

selectable.or({field: 1}, {field: 2})

Same as previous example but using the deprecated array wrap.

selectable.or([{field: 1}, {field: 2}])

Same as previous example, also deprecated.

selectable.or([{field: 1}], [{field: 2}])

Parameters:

  • criteria (Hash | Criteria | Array<Hash | Criteria>, ...)

    Multiple key/value pair matches or Criteria objects, or arrays thereof. Passing arrays is deprecated.

Returns:

Since:

  • 1.0.0



641
642
643
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 641

def or(*criteria)
  _mongoid_add_top_level_operation('$or', criteria)
end

#text_search(terms, opts = nil) ⇒ Selectable

Note:

Per docs.mongodb.com/manual/reference/operator/query/text/ it is not currently possible to supply multiple text search conditions in a query. Mongoid will build such a query but the server will return an error when trying to execute it.

Construct a text search selector.

Examples:

Construct a text search selector.

selectable.text_search("testing")

Construct a text search selector with options.

selectable.text_search("testing", :$language => "fr")

Parameters:

  • terms (String, Symbol)

    A string of terms that MongoDB parses and uses to query the text index.

  • opts (Hash) (defaults to: nil)

    Text search options. See MongoDB documentation for options.

Returns:

Since:

  • 2.2.0



790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 790

def text_search(terms, opts = nil)
  if terms.nil?
    raise Errors::CriteriaArgumentRequired, :terms
  end

  clone.tap do |query|
    criterion = {'$text' => { '$search' => terms }}
    criterion['$text'].merge!(opts) if opts
    if query.selector['$text']
      # Per https://docs.mongodb.com/manual/reference/operator/query/text/
      # multiple $text expressions are not currently supported by
      # MongoDB server, but build the query correctly instead of
      # overwriting previous text search condition with the currently
      # given one.
      Mongoid.logger.warn('Multiple $text expressions per query are not currently supported by the server')
      query.selector = {'$and' => [query.selector]}.merge(criterion)
    else
      query.selector = query.selector.merge(criterion)
    end
  end
end

#where(*criteria) ⇒ Selectable

This is the general entry point for most MongoDB queries. This either creates a standard field: value selection, and expanded selection with the use of hash methods, or a $where selection if a string is provided.

Examples:

Add a standard selection.

selectable.where(name: "syd")

Add a javascript selection.

selectable.where("this.name == 'syd'")

Parameters:

  • criterion (String, Hash)

    The javascript or standard selection.

Returns:

Since:

  • 1.0.0



827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 827

def where(*criteria)
  criteria.inject(clone) do |query, criterion|
    if criterion.nil?
      raise Errors::CriteriaArgumentRequired, :where
    end

    # We need to save the criterion in an instance variable so
    # Modifiable methods know how to create a polymorphic object.
    # Note that this method in principle accepts multiple criteria,
    # but only the first one will be stored in @criterion. This
    # works out to be fine because first_or_create etc. methods
    # only ever specify one criterion to #where.
    @criterion = criterion
    if criterion.is_a?(String)
      js_query(criterion)
    else
      expr_query(criterion)
    end
  end.reset_strategies!
end

#with_size(criterion) ⇒ Selectable

Note:

This method is named #with_size not to conflict with any existing #size method on enumerables or symbols.

Add a $size selection for array fields.

Examples:

Add the $size selection.

selectable.with_size(field: 5)

Execute an $size in a where query.

selectable.where(:field.with_size => 10)

Parameters:

  • criterion (Hash)

    The field/size pairs criterion.

Returns:

Since:

  • 1.0.0



728
729
730
731
732
733
734
735
736
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 728

def with_size(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :with_size
  end

  typed_override(criterion, "$size") do |value|
    ::Integer.evolve(value)
  end
end

#with_type(criterion) ⇒ Selectable

Note:

vurl.me/PGOU contains a list of all types.

Adds a $type selection to the selectable.

Examples:

Add the $type selection.

selectable.with_type(field: 15)

Execute an $type in a where query.

selectable.where(:field.with_type => 15)

Parameters:

  • criterion (Hash)

    The field/type pairs.

Returns:

Since:

  • 1.0.0



756
757
758
759
760
761
762
763
764
# File 'build/mongoid-7.1/lib/mongoid/criteria/queryable/selectable.rb', line 756

def with_type(criterion)
  if criterion.nil?
    raise Errors::CriteriaArgumentRequired, :with_type
  end

  typed_override(criterion, "$type") do |value|
    ::Integer.evolve(value)
  end
end