Module: Mongoid::Criteria::Queryable::Selectable
- Extended by:
- Macroable
- Defined in:
- 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.
"LineString"
- POINT =
Constant for a Point $geometry.
"Point"
- POLYGON =
Constant for a Polygon $geometry.
"Polygon"
Instance Attribute Summary collapse
-
#negating ⇒ Object
Returns the value of attribute negating.
- #negating If the next spression is negated.(Ifthe) ⇒ Object
-
#selector ⇒ Object
Returns the value of attribute selector.
- #selector The query selector.(Thequeryselector.) ⇒ Object
Class Method Summary collapse
-
.forwardables ⇒ Array<Symbol>
Get the methods on the selectable that can be forwarded to from a model.
Instance Method Summary collapse
-
#all(criterion = nil) ⇒ Selectable
(also: #all_in)
Add the $all criterion.
-
#and(*criterion) ⇒ Selectable
(also: #all_of)
Add the $and criterion.
-
#between(criterion = nil) ⇒ Selectable
Add the range selection.
-
#elem_match(criterion = nil) ⇒ Selectable
Select with an $elemMatch.
-
#exists(criterion = nil) ⇒ Selectable
Add the $exists selection.
-
#geo_spacial(criterion = nil) ⇒ Selectable
Add a $geoIntersects or $geoWithin selection.
-
#gt(criterion = nil) ⇒ Selectable
Add the $gt criterion to the selector.
-
#gte(criterion = nil) ⇒ Selectable
Add the $gte criterion to the selector.
-
#in(criterion = nil) ⇒ Selectable
(also: #any_in)
Adds the $in selection to the selectable.
-
#lt(criterion = nil) ⇒ Selectable
Add the $lt criterion to the selector.
-
#lte(criterion = nil) ⇒ Selectable
Add the $lte criterion to the selector.
-
#max_distance(criterion = nil) ⇒ Selectable
Add a $maxDistance selection to the selectable.
-
#mod(criterion = nil) ⇒ Selectable
Adds $mod selection to the selectable.
-
#ne(criterion = nil) ⇒ Selectable
(also: #excludes)
Adds $ne selection to the selectable.
-
#near(criterion = nil) ⇒ Selectable
Adds a $near criterion to a geo selection.
-
#near_sphere(criterion = nil) ⇒ Selectable
Adds a $nearSphere criterion to a geo selection.
-
#negating? ⇒ true, false
Is the current selectable negating the next selection?.
-
#nin(criterion = nil) ⇒ Selectable
(also: #not_in)
Adds the $nin selection to the selectable.
-
#nor(*criterion) ⇒ Selectable
Adds $nor selection to the selectable.
-
#not(*criterion) ⇒ Selectable
Negate the next selection.
-
#or(*criterion) ⇒ Selectable
(also: #any_of)
Adds $or selection to the selectable.
-
#text_search(terms, opts = nil) ⇒ Selectable
Construct a text search selector.
-
#where(criterion = nil) ⇒ Selectable
This is the general entry point for most MongoDB queries.
-
#with_size(criterion = nil) ⇒ Selectable
Add a $size selection for array fields.
-
#with_type(criterion = nil) ⇒ Selectable
Adds a $type selection to the selectable.
Methods included from Macroable
Instance Attribute Details
#negating ⇒ Object
Returns the value of attribute negating.
29 30 31 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 29 def negating @negating end |
#negating If the next spression is negated.(Ifthe) ⇒ Object
29 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 29 attr_accessor :negating, :selector |
#selector ⇒ Object
Returns the value of attribute selector.
29 30 31 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 29 def selector @selector end |
#selector The query selector.(Thequeryselector.) ⇒ Object
29 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 29 attr_accessor :negating, :selector |
Class Method Details
.forwardables ⇒ Array<Symbol>
Get the methods on the selectable that can be forwarded to from a model.
684 685 686 687 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 684 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.
44 45 46 |
# File '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.
61 62 63 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 61 def and(*criterion) __multi__(criterion, "$and") end |
#between(criterion = nil) ⇒ Selectable
Add the range selection.
79 80 81 82 83 84 85 86 |
# File '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.
107 108 109 |
# File '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.
128 129 130 131 132 |
# File '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
The only valid geometry shapes for a $geoIntersects are: :intersects_line, :intersects_point, and :intersects_polygon.
The only valid options for a $geoWithin query are the geometry shape :within_polygon and the operator :within_box.
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.
173 174 175 |
# File '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.
203 204 205 |
# File '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.
221 222 223 |
# File '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.
242 243 244 |
# File '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.
261 262 263 |
# File '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.
279 280 281 |
# File '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.
294 295 296 |
# File '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.
311 312 313 |
# File '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.
329 330 331 |
# File '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.
348 349 350 |
# File '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.
366 367 368 |
# File '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?
415 416 417 |
# File '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.
387 388 389 |
# File '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.
403 404 405 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 403 def nor(*criterion) __multi__(criterion, "$nor") end |
#not(*criterion) ⇒ Selectable
Negate the next selection.
435 436 437 438 439 440 441 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 435 def not(*criterion) if criterion.empty? 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.
454 455 456 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 454 def or(*criterion) __multi__(criterion, "$or") end |
#text_search(terms, opts = nil) ⇒ Selectable
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.
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
# File '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.
564 565 566 567 568 569 |
# File '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
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.
475 476 477 478 479 |
# File '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
vurl.me/PGOU contains a list of all types.
Adds a $type selection to the selectable.
499 500 501 502 503 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 499 def with_type(criterion = nil) typed_override(criterion, "$type") do |value| ::Integer.evolve(value) end end |