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'- ALLOWED_QUERY_OPERATORS =
Operators permitted at the top level of a query expression without opt-in. Excludes $where (JS execution) and other operators not needed for ordinary application queries.
%w[ $and $or $nor $not $text $comment $expr $jsonSchema $alwaysFalse $alwaysTrue ].freeze
- JAVASCRIPT_QUERY_OPERATORS =
Operators that execute server-side JavaScript. These are rejected at any depth, not just at the top level: the allowlist above permits $expr and the logical operators, and their values are arbitrary nested expressions that can carry $function or $where.
%w[$where $function $accumulator].freeze
Instance Attribute Summary collapse
-
#negating ⇒ Object
Returns the value of attribute negating.
- #negating If the next expression 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(*criteria) ⇒ Selectable
(also: #all_in)
Add the $all criterion.
-
#and(*criteria) ⇒ Selectable
(also: #all_of)
Add the $and criterion.
-
#any_of(*criteria) ⇒ Selectable
Adds a disjunction of the arguments as an additional constraint to the criteria already existing in the receiver.
-
#between(criterion) ⇒ Selectable
Add the range selection.
-
#elem_match(criterion) ⇒ Selectable
Select with an $elemMatch.
-
#eq(criterion) ⇒ Selectable
Add the $eq criterion to the selector.
-
#exists(criterion) ⇒ Selectable
Add the $exists selection.
-
#expr_query(criterion) ⇒ Selectable
private
Adds the specified expression to the query.
-
#geo_spatial(criterion) ⇒ Selectable
Add a $geoIntersects or $geoWithin selection.
-
#gt(criterion) ⇒ Selectable
Add the $gt criterion to the selector.
-
#gte(criterion) ⇒ Selectable
Add the $gte criterion to the selector.
-
#in(condition) ⇒ Selectable
(also: #any_in)
Adds the $in selection to the selectable.
-
#js_query(criterion) ⇒ Selectable
private
Create a javascript selection.
-
#lt(criterion) ⇒ Selectable
Add the $lt criterion to the selector.
-
#lte(criterion) ⇒ Selectable
Add the $lte criterion to the selector.
-
#max_distance(criterion) ⇒ Selectable
Add a $maxDistance selection to the selectable.
-
#mod(criterion) ⇒ Selectable
Adds $mod selection to the selectable.
-
#ne(criterion) ⇒ Selectable
(also: #excludes)
Adds $ne selection to the selectable.
-
#near(criterion) ⇒ Selectable
Adds a $near criterion to a geo selection.
-
#near_sphere(criterion) ⇒ Selectable
Adds a $nearSphere criterion to a geo selection.
-
#negating? ⇒ true | false
Is the current selectable negating the next selection?.
-
#nin(condition) ⇒ Selectable
(also: #not_in)
Adds the $nin selection to the selectable.
-
#none_of(*criteria) ⇒ Selectable
Negate the arguments, constraining the query to only those documents that do NOT match the arguments.
-
#nor(*criteria) ⇒ Selectable
Adds $nor selection to the selectable.
-
#not(*criteria) ⇒ Selectable
Negate the arguments.
-
#or(*criteria) ⇒ Selectable
Creates a disjunction using $or from the existing criteria in the receiver and the provided arguments.
-
#text_search(terms, opts = nil) ⇒ Selectable
Construct a text search selector.
-
#where(*criteria) ⇒ Selectable
This is the general entry point for most MongoDB queries.
-
#with_size(criterion) ⇒ Selectable
Add a $size selection for array fields.
-
#with_type(criterion) ⇒ Selectable
Adds a $type selection to the selectable.
Methods included from Macroable
Instance Attribute Details
#negating ⇒ Object
Returns the value of attribute negating.
23 24 25 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 23 def negating @negating end |
#negating If the next expression is negated.(Ifthe) ⇒ Object
23 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 23 attr_accessor :negating, :selector |
#selector ⇒ Object
Returns the value of attribute selector.
23 24 25 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 23 def selector @selector end |
#selector The query selector.(Thequeryselector.) ⇒ Object
23 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 23 attr_accessor :negating, :selector |
Class Method Details
.forwardables ⇒ Array<Symbol>
Get the methods on the selectable that can be forwarded to from a model.
999 1000 1001 1002 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 999 def forwardables public_instance_methods(false) - i[negating negating= negating? selector selector=] end |
Instance Method Details
#all(*criteria) ⇒ Selectable Also known as: all_in
Add the $all criterion.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 36 def all(*criteria) if criteria.empty? return clone.tap do |query| query.reset_strategies! end end criteria.inject(clone) do |query, condition| raise Errors::CriteriaArgumentRequired, :all if condition.nil? condition = (condition) if strategy send(strategy, condition, '$all') else condition.inject(query) do |_query, (field, value)| v = { '$all' => value } v = { '$not' => v } if negating? _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.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 72 def and(*criteria) _mongoid_flatten_arrays(criteria).inject(clone) do |c, new_s| new_s = new_s.selector if new_s.is_a?(Selectable) normalized = (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?('$') } 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.
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 633 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) (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 self.and('$or' => exprs) end end |
#between(criterion) ⇒ Selectable
Add the range selection.
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 115 def between(criterion) raise Errors::CriteriaArgumentRequired, :between if criterion.nil? 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.
143 144 145 146 147 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 143 def elem_match(criterion) raise Errors::CriteriaArgumentRequired, :elem_match if criterion.nil? and_with_operator(criterion, '$elemMatch') end |
#eq(criterion) ⇒ Selectable
Add the $eq criterion to the selector.
240 241 242 243 244 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 240 def eq(criterion) raise Errors::CriteriaArgumentRequired, :eq if criterion.nil? and_with_operator(criterion, '$eq') end |
#exists(criterion) ⇒ Selectable
Add the $exists selection.
164 165 166 167 168 169 170 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 164 def exists(criterion) raise Errors::CriteriaArgumentRequired, :exists if criterion.nil? typed_override(criterion, '$exists') do |value| Mongoid::Boolean.evolve(value) end end |
#expr_query(criterion) ⇒ Selectable
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds the specified expression to the query.
Criterion must be a hash in one of the following forms:
- value
- => value
- value
- => operator_value_expression
Field name and operator may be given as either strings or symbols.
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 822 def expr_query(criterion) raise ArgumentError, 'Criterion cannot be nil here' if criterion.nil? unless criterion.is_a?(Hash) raise Errors::InvalidQuery, "Expression must be a Hash: #{Errors::InvalidQuery.truncate_expr(criterion)}" end # The operator guard is applied by _mongoid_expand_keys, which every # query method that accepts a user-supplied expression passes through. normalized = (criterion) clone.tap do |query| normalized.each do |field, value| field_s = field.to_s if field_s.start_with?('$') query.add_operator_expression(field_s, value) else query.add_field_expression(field, value) end end query.reset_strategies! end end |
#geo_spatial(criterion) ⇒ 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: https://www.mongodb.com/docs/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.
209 210 211 212 213 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 209 def geo_spatial(criterion) raise Errors::CriteriaArgumentRequired, :geo_spatial if criterion.nil? __merge__(criterion) end |
#gt(criterion) ⇒ Selectable
Add the $gt criterion to the selector.
258 259 260 261 262 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 258 def gt(criterion) raise Errors::CriteriaArgumentRequired, :gt if criterion.nil? and_with_operator(criterion, '$gt') end |
#gte(criterion) ⇒ Selectable
Add the $gte criterion to the selector.
276 277 278 279 280 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 276 def gte(criterion) raise Errors::CriteriaArgumentRequired, :gte if criterion.nil? and_with_operator(criterion, '$gte') end |
#in(condition) ⇒ Selectable Also known as: any_in
Adds the $in selection to the selectable.
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 297 def in(condition) raise Errors::CriteriaArgumentRequired, :in if condition.nil? condition = (condition) if strategy send(strategy, condition, '$in') else condition.inject(clone) do |query, (field, value)| v = { '$in' => value } v = { '$not' => v } if negating? query.add_field_expression(field.to_s, v) end.reset_strategies! end end |
#js_query(criterion) ⇒ Selectable
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a javascript selection.
854 855 856 857 858 859 860 861 862 863 864 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 854 def js_query(criterion) clone.tap do |query| if negating? query.add_operator_expression('$and', [ { '$nor' => [ { '$where' => criterion } ] } ]) else query.add_operator_expression('$where', criterion) end query.reset_strategies! end end |
#lt(criterion) ⇒ Selectable
Add the $lt criterion to the selector.
326 327 328 329 330 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 326 def lt(criterion) raise Errors::CriteriaArgumentRequired, :lt if criterion.nil? and_with_operator(criterion, '$lt') end |
#lte(criterion) ⇒ Selectable
Add the $lte criterion to the selector.
344 345 346 347 348 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 344 def lte(criterion) raise Errors::CriteriaArgumentRequired, :lte if criterion.nil? and_with_operator(criterion, '$lte') end |
#max_distance(criterion) ⇒ Selectable
Add a $maxDistance selection to the selectable.
359 360 361 362 363 364 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 359 def max_distance(criterion) raise Errors::CriteriaArgumentRequired, :max_distance if criterion.nil? # $maxDistance must be given together with $near __add__(criterion, '$maxDistance') end |
#mod(criterion) ⇒ Selectable
Adds $mod selection to the selectable.
377 378 379 380 381 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 377 def mod(criterion) raise Errors::CriteriaArgumentRequired, :mod if criterion.nil? and_with_operator(criterion, '$mod') end |
#ne(criterion) ⇒ Selectable Also known as: excludes
Adds $ne selection to the selectable.
395 396 397 398 399 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 395 def ne(criterion) raise Errors::CriteriaArgumentRequired, :ne if criterion.nil? and_with_operator(criterion, '$ne') end |
#near(criterion) ⇒ Selectable
Adds a $near criterion to a geo selection.
414 415 416 417 418 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 414 def near(criterion) raise Errors::CriteriaArgumentRequired, :near if criterion.nil? and_with_operator(criterion, '$near') end |
#near_sphere(criterion) ⇒ Selectable
Adds a $nearSphere criterion to a geo selection.
432 433 434 435 436 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 432 def near_sphere(criterion) raise Errors::CriteriaArgumentRequired, :near_sphere if criterion.nil? and_with_operator(criterion, '$nearSphere') end |
#negating? ⇒ true | false
Is the current selectable negating the next selection?
490 491 492 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 490 def negating? !!negating end |
#nin(condition) ⇒ Selectable Also known as: not_in
Adds the $nin selection to the selectable.
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 453 def nin(condition) raise Errors::CriteriaArgumentRequired, :nin if condition.nil? condition = (condition) if strategy send(strategy, condition, '$nin') else condition.inject(clone) do |query, (field, value)| v = { '$nin' => value } v = { '$not' => v } if negating? query.add_field_expression(field.to_s, v) end.reset_strategies! end end |
#none_of(*criteria) ⇒ Selectable
Negate the arguments, constraining the query to only those documents that do NOT match the arguments.
560 561 562 563 564 565 566 567 568 569 570 571 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 560 def none_of(*criteria) criteria = _mongoid_flatten_arrays(criteria) return dup if criteria.empty? exprs = criteria.map do |criterion| ( criterion.is_a?(Selectable) ? criterion.selector : criterion ) end self.and('$nor' => exprs) end |
#nor(*criteria) ⇒ Selectable
Adds $nor selection to the selectable.
480 481 482 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 480 def nor(*criteria) _mongoid_add_top_level_operation('$nor', criteria) end |
#not(*criteria) ⇒ Selectable
Negate the arguments.
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 509 def not(*criteria) if criteria.empty? # @deprecated Mongoid.deprecation_warning( :not_sans_arguments, 'Calling `#not` without arguments is deprecated, and will be ' \ 'removed in the next major version. Instead, pass the Hash or ' \ 'Criteria instance to negate.', caller_locations ) dup.tap { |query| query.negating = !query.negating } else criteria.compact.inject(clone) do |c, new_s| new_s = new_s.selector if new_s.is_a?(Selectable) (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') elsif v.is_a?(Hash) c = c.send(:__multi__, [ { '$nor' => [ { k => v } ] } ], '$and') else negated_operator = if v.is_a?(Regexp) '$not' else '$ne' end c = c.send(:__override__, { k => v }, negated_operator) 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.
603 604 605 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 603 def or(*criteria) _mongoid_add_top_level_operation('$or', criteria) end |
#text_search(terms, opts = nil) ⇒ Selectable
Per https://www.mongodb.com/docs/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.
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 731 def text_search(terms, opts = nil) raise Errors::CriteriaArgumentRequired, :terms if terms.nil? clone.tap do |query| criterion = { '$text' => { '$search' => terms } } criterion['$text'].merge!(opts) if opts if query.selector['$text'] # Per https://www.mongodb.com/docs/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.
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 765 def where(*criteria) return clone.reset_strategies! if criteria.empty? criteria.inject(self) do |query, criterion| raise Errors::CriteriaArgumentRequired, :where if criterion.nil? # 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) unless Mongoid.allow_unsafe_query_operators? raise Errors::InvalidQuery, "String criteria are not allowed because they compile to the '$where' operator, " \ 'which is not allowed in a query expression. Set Mongoid.allow_unsafe_query_operators = true ' \ 'to permit all operators.' end query.js_query(criterion) else query.expr_query(criterion) end end end |
#with_size(criterion) ⇒ 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.
677 678 679 680 681 682 683 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 677 def with_size(criterion) raise Errors::CriteriaArgumentRequired, :with_size if criterion.nil? typed_override(criterion, '$size') do |value| ::Integer.evolve(value) end end |
#with_type(criterion) ⇒ Selectable
http://vurl.me/PGOU contains a list of all types.
Adds a $type selection to the selectable.
701 702 703 704 705 706 707 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 701 def with_type(criterion) raise Errors::CriteriaArgumentRequired, :with_type if criterion.nil? typed_override(criterion, '$type') do |value| ::Integer.evolve(value) end end |