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

Extended by:
Macroable
Defined in:
build/mongoid-7.0/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.



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

def negating
  @negating
end

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



29
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 29

attr_accessor :negating, :selector

#selectorObject

Returns the value of attribute selector.



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

def selector
  @selector
end

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



29
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 29

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



683
684
685
686
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 683

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

Instance Method Details

#all(criterion = nil) ⇒ 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) (defaults to: nil)

    The key value pairs for $all matching.

Returns:

Since:

  • 1.0.0



44
45
46
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 44

def all(criterion = nil)
  send(strategy || :__union__, with_array_values(criterion), "$all")
end

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

Add the $and criterion.

Examples:

Add the criterion.

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

Parameters:

  • criterion (Array<Hash>)

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

Returns:

Since:

  • 1.0.0



61
62
63
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 61

def and(*criterion)
  __multi__(criterion, "$and")
end

#between(criterion = nil) ⇒ 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) (defaults to: nil)

    Multiple key/range pairs.

Returns:

Since:

  • 1.0.0



79
80
81
82
83
84
85
86
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 79

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

#elem_match(criterion = nil) ⇒ 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) (defaults to: nil)

    The field/match pairs.

Returns:

Since:

  • 1.0.0



107
108
109
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 107

def elem_match(criterion = nil)
  __override__(criterion, "$elemMatch")
end

#exists(criterion = nil) ⇒ 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) (defaults to: nil)

    The field/boolean existence checks.

Returns:

Since:

  • 1.0.0



128
129
130
131
132
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 128

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

#geo_spacial(criterion = nil) ⇒ 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) (defaults to: nil)

    The criterion.

Returns:

Since:

  • 2.0.0



173
174
175
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 173

def geo_spacial(criterion = nil)
  __merge__(criterion)
end

#gt(criterion = nil) ⇒ 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) (defaults to: nil)

    The field/value pairs to check.

Returns:

Since:

  • 1.0.0



203
204
205
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 203

def gt(criterion = nil)
  __override__(criterion, "$gt")
end

#gte(criterion = nil) ⇒ 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) (defaults to: nil)

    The field/value pairs to check.

Returns:

Since:

  • 1.0.0



221
222
223
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 221

def gte(criterion = nil)
  __override__(criterion, "$gte")
end

#in(criterion = nil) ⇒ 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:

  • criterion (Hash) (defaults to: nil)

    The field/value criterion pairs.

Returns:

Since:

  • 1.0.0



242
243
244
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 242

def in(criterion = nil)
  send(strategy || :__intersect__, with_array_values(criterion), "$in")
end

#lt(criterion = nil) ⇒ 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) (defaults to: nil)

    The field/value pairs to check.

Returns:

Since:

  • 1.0.0



261
262
263
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 261

def lt(criterion = nil)
  __override__(criterion, "$lt")
end

#lte(criterion = nil) ⇒ 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) (defaults to: nil)

    The field/value pairs to check.

Returns:

Since:

  • 1.0.0



279
280
281
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 279

def lte(criterion = nil)
  __override__(criterion, "$lte")
end

#max_distance(criterion = nil) ⇒ Selectable

Add a $maxDistance selection to the selectable.

Examples:

Add the $maxDistance selection.

selectable.max_distance(location: 10)

Parameters:

  • criterion (Hash) (defaults to: nil)

    The field/distance pairs.

Returns:

Since:

  • 1.0.0



294
295
296
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 294

def max_distance(criterion = nil)
  __add__(criterion, "$maxDistance")
end

#mod(criterion = nil) ⇒ 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) (defaults to: nil)

    The field/mod selections.

Returns:

Since:

  • 1.0.0



311
312
313
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 311

def mod(criterion = nil)
  __override__(criterion, "$mod")
end

#ne(criterion = nil) ⇒ 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) (defaults to: nil)

    The field/ne selections.

Returns:

Since:

  • 1.0.0



329
330
331
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 329

def ne(criterion = nil)
  __override__(criterion, "$ne")
end

#near(criterion = nil) ⇒ 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) (defaults to: nil)

    The field/location pair.

Returns:

Since:

  • 1.0.0



348
349
350
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 348

def near(criterion = nil)
  __override__(criterion, "$near")
end

#near_sphere(criterion = nil) ⇒ 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) (defaults to: nil)

    The field/location pair.

Returns:

Since:

  • 1.0.0



366
367
368
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 366

def near_sphere(criterion = nil)
  __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



415
416
417
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 415

def negating?
  !!negating
end

#nin(criterion = nil) ⇒ 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:

  • criterion (Hash) (defaults to: nil)

    The field/value criterion pairs.

Returns:

Since:

  • 1.0.0



387
388
389
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 387

def nin(criterion = nil)
  send(strategy || :__intersect__, with_array_values(criterion), "$nin")
end

#nor(*criterion) ⇒ Selectable

Adds $nor selection to the selectable.

Examples:

Add the $nor selection.

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

Parameters:

  • criterion (Array)

    An array of hash criterion.

Returns:

Since:

  • 1.0.0



403
404
405
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 403

def nor(*criterion)
  __multi__(criterion, "$nor")
end

#not(*criterion) ⇒ Selectable

Negate the next selection.

Examples:

Negate the 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:

  • criterion (Hash)

    The field/value pairs to negate.

Returns:

Since:

  • 1.0.0



435
436
437
438
439
440
441
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 435

def not(*criterion)
  if criterion.empty?
    dup.tap { |query| query.negating = true }
  else
    __override__(criterion.first, "$not")
  end
end

#or(*criterion) ⇒ Selectable Also known as: any_of

Adds $or selection to the selectable.

Examples:

Add the $or selection.

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

Parameters:

  • criterion (Array)

    An array of hash criterion.

Returns:

Since:

  • 1.0.0



454
455
456
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 454

def or(*criterion)
  __multi__(criterion, "$or")
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



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 529

def text_search(terms, opts = nil)
  clone.tap do |query|
    if terms
      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
end

#where(criterion = nil) ⇒ 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) (defaults to: nil)

    The javascript or standard selection.

Returns:

Since:

  • 1.0.0



564
565
566
567
568
569
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 564

def where(criterion = nil)
  # We need to save the criterion in an instance variable so Modifiable methods
  # know how to create a polymorphic object.
  @criterion = criterion
  criterion.is_a?(String) ? js_query(criterion) : expr_query(criterion)
end

#with_size(criterion = nil) ⇒ 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) (defaults to: nil)

    The field/size pairs criterion.

Returns:

Since:

  • 1.0.0



475
476
477
478
479
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 475

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

#with_type(criterion = nil) ⇒ 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) (defaults to: nil)

    The field/type pairs.

Returns:

Since:

  • 1.0.0



499
500
501
502
503
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/selectable.rb', line 499

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