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 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.
-
#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.
-
#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 the next selection if no arguments are given.
-
#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.
25 26 27 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 25 def negating @negating end |
#negating If the next expression is negated.(Ifthe) ⇒ Object
25 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 25 attr_accessor :negating, :selector |
#selector ⇒ Object
Returns the value of attribute selector.
25 26 27 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 25 def selector @selector end |
#selector The query selector.(Thequeryselector.) ⇒ Object
25 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 25 attr_accessor :negating, :selector |
Class Method Details
.forwardables ⇒ Array<Symbol>
Get the methods on the selectable that can be forwarded to from a model.
945 946 947 948 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 945 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.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 38 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 = (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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 78 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 = (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.
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 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 674 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.
124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 124 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.
154 155 156 157 158 159 160 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 154 def elem_match(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :elem_match end and_with_operator(criterion, "$elemMatch") end |
#eq(criterion) ⇒ Selectable
Add the $eq criterion to the selector.
257 258 259 260 261 262 263 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 257 def eq(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :eq end and_with_operator(criterion, "$eq") end |
#exists(criterion) ⇒ Selectable
Add the $exists selection.
177 178 179 180 181 182 183 184 185 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 177 def exists(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :exists end typed_override(criterion, "$exists") do |value| Mongoid::Boolean.evolve(value) 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: 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.
224 225 226 227 228 229 230 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 224 def geo_spatial(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :geo_spatial end __merge__(criterion) end |
#gt(criterion) ⇒ Selectable
Add the $gt criterion to the selector.
277 278 279 280 281 282 283 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 277 def gt(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :gt end and_with_operator(criterion, "$gt") end |
#gte(criterion) ⇒ Selectable
Add the $gte criterion to the selector.
297 298 299 300 301 302 303 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 297 def gte(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :gte end and_with_operator(criterion, "$gte") end |
#in(condition) ⇒ Selectable Also known as: any_in
Adds the $in selection to the selectable.
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 320 def in(condition) if condition.nil? raise Errors::CriteriaArgumentRequired, :in end condition = (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.
353 354 355 356 357 358 359 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 353 def lt(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :lt end and_with_operator(criterion, "$lt") end |
#lte(criterion) ⇒ Selectable
Add the $lte criterion to the selector.
373 374 375 376 377 378 379 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 373 def lte(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :lte end and_with_operator(criterion, "$lte") end |
#max_distance(criterion) ⇒ Selectable
Add a $maxDistance selection to the selectable.
390 391 392 393 394 395 396 397 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 390 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.
410 411 412 413 414 415 416 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 410 def mod(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :mod end and_with_operator(criterion, "$mod") end |
#ne(criterion) ⇒ Selectable Also known as: excludes
Adds $ne selection to the selectable.
430 431 432 433 434 435 436 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 430 def ne(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :ne end and_with_operator(criterion, "$ne") end |
#near(criterion) ⇒ Selectable
Adds a $near criterion to a geo selection.
451 452 453 454 455 456 457 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 451 def near(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :near end and_with_operator(criterion, "$near") end |
#near_sphere(criterion) ⇒ Selectable
Adds a $nearSphere criterion to a geo selection.
471 472 473 474 475 476 477 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 471 def near_sphere(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :near_sphere end and_with_operator(criterion, "$nearSphere") end |
#negating? ⇒ true | false
Is the current selectable negating the next selection?
535 536 537 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 535 def negating? !!negating end |
#nin(condition) ⇒ Selectable Also known as: not_in
Adds the $nin selection to the selectable.
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 494 def nin(condition) if condition.nil? raise Errors::CriteriaArgumentRequired, :nin end condition = (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 |
#none_of(*criteria) ⇒ Selectable
Negate the arguments, constraining the query to only those documents that do NOT match the arguments.
601 602 603 604 605 606 607 608 609 610 611 612 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 601 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.
525 526 527 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 525 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.
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 554 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 (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.
644 645 646 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 644 def or(*criteria) _mongoid_add_top_level_operation('$or', criteria) end |
#text_search(terms, opts = nil) ⇒ Selectable
Per 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.
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 776 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://mongodb.prakticum-team.ru/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.
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 812 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
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.
718 719 720 721 722 723 724 725 726 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 718 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
vurl.me/PGOU contains a list of all types.
Adds a $type selection to the selectable.
744 745 746 747 748 749 750 751 752 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 744 def with_type(criterion) if criterion.nil? raise Errors::CriteriaArgumentRequired, :with_type end typed_override(criterion, "$type") do |value| ::Integer.evolve(value) end end |