模块:Mongoid::Criteria::Queryable::Selectable
- 扩展方式:
- 可宏
- 定义于:
- lib/mongoid/criteria/queryable/selectable.rb
Overview
Queryable Selectable 是可选择的,因为它能力从数据库中选择文档。 selectable 模块为 selectable 带来了与构建MongoDB选择器有关的所有功能。
常量摘要折叠
- LINE_STRING =
线串 $geometry 的常量。
'线串(LineString)'- POINT =
Point $geometry 常量。
'点'- POLYGON =
Polygon $geometry 常量。
'多边形'
实例属性摘要折叠
-
#取反"> 对象
返回属性否定值。
- # negating 如果下一个表达式是否定的。 (ifthe) ⇒ 对象
-
#选择器⇒ 对象
返回属性选择器的值。
- # selector 查询选择器。 (查询选择器。)⇒ 对象
类方法摘要折叠
-
。 Forwardables ⇒ Array<Symbol>
获取可从模型转发到的可选对象上的方法。
实例方法摘要折叠
-
# all (*criteria) ⇒ 可选(也:#all_in)
添加 $all 条件。
-
# and (*criteria) ⇒ 可选(也:#all_of)
添加 $and 条件。
-
# any_of (*criteria) ⇒ 可选
添加参数的析取,作为接收器中已存在条件的附加约束。
-
# between (criterion) ⇒ 可选
添加范围选择。
-
# elem_match (criterion) ⇒ 可选
使用 $elemMatch 进行选择。
-
# eq (criterion) ⇒ 可选
将 $eq 标准添加到选择器。
-
# exists (criterion) ⇒ 可选
添加 $exists 选项。
-
# expr_query (criterion) ⇒ 可选
private
将指定表达式添加到查询。
-
# geo_spatial (criterion) ⇒ 可选
添加 $geoIntersects 或 $geoWithin 选区。
-
# gt (criterion) ⇒ 可选
将 $gt 条件添加到选择器。
-
# gte (criterion) ⇒ 可选
将 $gte 标准添加到选择器。
-
# in (condition) ⇒ 可选择(也可:#any_in)
将 $in 选项添加到可选内容中。
-
#js_query (criterion) ⇒ 可选
private
创建 JavaScript 选区。
-
# lt (criterion) ⇒ 可选
将 $lt 条件添加到选择器。
-
# lte (criterion) ⇒ 可选
将 $lte 标准添加到选择器。
-
# max_distance (criterion) ⇒ 可选
将 $maxDistance 选择添加到可选对象中。
-
# mod (criterion) ⇒ 可选
将 $mod 选择添加到可选内容中。
-
# ne (criterion) ⇒ 可选(也:#excludes)
将 $ne 选择添加到可选择项中。
-
# near (criterion) ⇒ 可选
将 $near 标准添加到地理选择中。
-
# near_sphere (criterion) ⇒ 可选
将 $nearSphere 标准添加到地理选择中。
-
#否定? ⇒ true | false
当前可选选项是否会否定下一个选择?
-
# nin (条件)→ 可选(也:#not_in)
将 $nin 选项添加到可选择项中。
-
# none_of (*criteria) ⇒ 可选
对参数取反,将查询限制为仅查询与参数不匹配的文档。
-
# nor (*criteria) ⇒ 可选
将 $nor 选择添加到可选择项中。
-
# not (*criteria) ⇒ 可选
对参数取反,如果未给出参数,则对下一个选择取反。
-
# or (*criteria) ⇒ 可选
使用 $or 根据接收器中的现有条件和提供的参数创建一个或关系。
-
# text_search (terms, opts = nil) ⇒ 可选
构建文本Atlas Search选择器。
-
# where (*criteria) ⇒ 可选
这是大多数MongoDB查询的一般入口点。
-
# with_size (criterion) ⇒ 可选
为数组字段添加 $size 选择。
-
# with_type (criterion) ⇒ 可选
将 $type 选择添加到可选对象中。
从Macroable包含的方法
实例属性详细信息
#取反">对象
返回属性否定值。
23 24 25 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 23 def 否定 @negating end |
# negating 如果下一个表达式是否定的。 (如果) ⇒对象
23 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 23 attr_accessor :negating, :selector |
#选择器⇒对象
返回属性选择器的值。
23 24 25 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 23 def 选择器 @selector end |
# selector查询选择器。 (查询选择器。) ⇒对象
23 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 23 attr_accessor :negating, :selector |
类方法详细信息
。 Forwardables ⇒ Array<Symbol>
获取可从模型转发到的可选对象上的方法。
883 884 885 886 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 883 def Forwardables public_instance_methods(false) - %i[否定 negating= 否定? 选择器 SELECTOR =] end |
实例方法详细信息
# all (*criteria) ⇒ Selectable也称为: all_in
添加 $all 条件。
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 所有(*条件) if 条件.空? return 克隆.点击 do |查询| 查询.reset_strategies! end end 条件.注入(克隆) do |查询, 条件| 提高 Errors::CriteriaArgumentRequired, :all if 条件.nil? 条件 = (条件) if 策略 发送(策略, 条件, ' $all ') else 条件.注入(查询) do |_query, (字段, 值)| v = { ' $all ' => 值 } v = { ' $not ' => v } if 否定? _query.add_field_expression(字段.to_s, v) end end end.reset_strategies! end |
# and (*criteria) ⇒ Selectable也称为: all_of
添加 $and 条件。
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 和(*条件) _mongoid_flatten_arrays(条件).注入(克隆) do |C, new_s| new_s = new_s.选择器 if new_s.is_a?(可选) 标准化 = (new_s) 标准化.每 do |k, v| k = k.to_s if C.选择器[k] # k 上已经有一个条件。 # 如果 v 是操作符,并且所有现有条件都是 # 也是操作符,并且 v 在现有条件中不存在, # 我们可以添加到现有条件。 # 否则使用 $and。 if v.is_a?(哈希) && v.长度 == 1 && (new_k = v.密钥.first).start_with?('$') && (existing_kv = C.选择器[k]).is_a?(哈希) && !existing_kv.键?(new_k) && existing_kv.密钥.全部? { |sub_k| sub_k.start_with?('$') } merged_v = C.选择器[k].合并(merge)(v) C.选择器.存储(k, merged_v) else C = C.发送(:__multi__, [ { k => v } ], ' $and ') end else C.选择器.存储(k, v) end end C end end |
# any_of (*criteria) ⇒可选
添加参数的析取,作为接收器中已存在条件的附加约束。
使用or使接收者成为析取操作数之一。
每个参数都可以是哈希、条件对象、哈希或条件对象的大量或嵌套大量。 嵌套数组将被展平,并且可以是任意深度。 传递数组已被弃用。
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 625 def any_of(*条件) 条件 = _mongoid_flatten_arrays(条件) 案例 条件.长度 when 0 克隆 when 1 # 当我们有一个标准时,any_of 的行为类似于 and。 # 注意:条件可以是查询对象,#where 方法执行此操作 # 不支持. self.和(*条件) else # 当我们有多个条件时,用 $or 将它们全部组合起来 # 并将结果添加到 self. 表达式 = 条件.map do |criterion| if criterion.is_a?(可选) (criterion.选择器) else 哈希[criterion.map do |k, v| if k.is_a?(符号) [ k.to_s, v ] else [ k, v ] end end] end end self.和(' $or ' => 表达式) end end |
# between (criterion) ⇒可选
添加范围选择。
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 115 def between(criterion) 提高 Errors::CriteriaArgumentRequired, : between if criterion.nil? 选择(criterion) do |选择器, 字段, 值| 选择器.存储( 字段, { '$gte' => 值.min, ' $lte ' => 值.Max } ) end end |
# elem_match (criterion) ⇒可选
使用 $elemMatch 进行选择。
143 144 145 146 147 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 143 def elem_match(criterion) 提高 Errors::CriteriaArgumentRequired, :elem_match if criterion.nil? and_with_operator(criterion, '$elemMatch') end |
# eq (criterion) ⇒可选
将 $eq 标准添加到选择器。
240 241 242 243 244 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 240 def eq(criterion) 提高 Errors::CriteriaArgumentRequired, :eq if criterion.nil? and_with_operator(criterion, '$eq') end |
# exists (criterion) ⇒可选
添加 $exists 选项。
164 165 166 167 168 169 170 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 164 def 存在(criterion) 提高 Errors::CriteriaArgumentRequired, :exists if criterion.nil? typed_override(criterion, ' $exists ') do |值| Mongoid::布尔.发展(值) end end |
# expr_query (criterion) ⇒ 可选
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
将指定表达式添加到查询。
标准必须是以下形式之一的哈希值:
- 值
- => 值
- 值
- => Operator_value_expression
字段名称和操作符可以字符串或符号的形式给出。
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 795 def expr_query(criterion) 提高 ArgumentError, ' 这里的条件不能为 nil ' if criterion.nil? 除非 criterion.is_a?(哈希) 提高 Errors::InvalidQuery, "表达式必须是哈希: #{Errors::InvalidQuery.truncate_expr(Criterion)}" end 标准化 = (criterion) 克隆.点击 do |查询| 标准化.每 do |字段, 值| field_s = 字段.to_s if field_s.start_with?('$') # 查询表达式级操作符,如 $and 或 $where 查询.add_operator_expression(field_s, 值) else 查询.add_field_expression(字段, 值) end end 查询.reset_strategies! end end |
# geo_spatial (criterion) ⇒可选
$geoIntersects 的唯一有效几何形状是::intersects_line、:intersects_point 和 :intersects_polygon。
$geoWithin 查询的唯一有效选项是几何形状 :within_polygon 和运算符 :within_box。
$geoWithin查询的 :within_box操作符期望左下(西南)坐标对作为第一个参数,右上角(东北)坐标对作为第二个参数。重要提示:传递纬度和经度时,经度应为坐标对的第一个元素。来源: https: 操作符
添加 $geoIntersects 或 $geoWithin 选区。 必须按示例所示使用符号操作符来扩展条件。
209 210 211 212 213 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 209 def geo_spatial(criterion) 提高 Errors::CriteriaArgumentRequired, :geo_spatial if criterion.nil? __merge__(criterion) end |
# gt (criterion) ⇒可选
将 $gt 条件添加到选择器。
258 259 260 261 262 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 258 def gt(criterion) 提高 Errors::CriteriaArgumentRequired, :gt if criterion.nil? and_with_operator(criterion, '$gt') end |
# gte (criterion) ⇒可选
将 $gte 标准添加到选择器。
276 277 278 279 280 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 276 def gte(criterion) 提高 Errors::CriteriaArgumentRequired, :gte if criterion.nil? and_with_operator(criterion, '$gte') end |
# in (condition) ⇒ Selectable也称为: any_in
将 $in 选项添加到可选内容中。
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(条件) 提高 Errors::CriteriaArgumentRequired, :in if 条件.nil? 条件 = (条件) if 策略 发送(策略, 条件, ' $in ') else 条件.注入(克隆) do |查询, (字段, 值)| v = { ' $in ' => 值 } v = { ' $not ' => v } if 否定? 查询.add_field_expression(字段.to_s, v) end.reset_strategies! end end |
#js_query (criterion) ⇒ 可选
此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。
创建 JavaScript 选区。
826 827 828 829 830 831 832 833 834 835 836 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 826 def js_query(criterion) 克隆.点击 do |查询| if 否定? 查询.add_operator_expression(' $and ', [ { ' $nor ' => [ { ' $where ' => criterion } ] } ]) else 查询.add_operator_expression(' $where ', criterion) end 查询.reset_strategies! end end |
# lt (criterion) ⇒可选
将 $lt 条件添加到选择器。
326 327 328 329 330 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 326 def lt(criterion) 提高 Errors::CriteriaArgumentRequired, :lt if criterion.nil? and_with_operator(criterion, '$lt') end |
# lte (criterion) ⇒可选
将 $lte 标准添加到选择器。
344 345 346 347 348 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 344 def lte(criterion) 提高 Errors::CriteriaArgumentRequired, :lte if criterion.nil? and_with_operator(criterion, ' $lte ') end |
# max_distance (criterion) ⇒可选
将 $maxDistance 选择添加到可选对象中。
359 360 361 362 363 364 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 359 def max_distance(criterion) 提高 Errors::CriteriaArgumentRequired, :max_distance if criterion.nil? # $maxDistance 必须与 $near 一起给出 __add__(criterion, ' $maxDistance ') end |
# mod (criterion) ⇒可选
将 $mod 选择添加到可选内容中。
377 378 379 380 381 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 377 def mod(criterion) 提高 Errors::CriteriaArgumentRequired, :mod if criterion.nil? and_with_operator(criterion, '$mod') end |
# ne (criterion) ⇒ Selectable也称为:排除
将 $ne 选择添加到可选择项中。
395 396 397 398 399 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 395 def ne(criterion) 提高 Errors::CriteriaArgumentRequired, :ne if criterion.nil? and_with_operator(criterion, '$ne') end |
# near (criterion) ⇒可选
将 $near 标准添加到地理选择中。
414 415 416 417 418 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 414 def 接近(criterion) 提高 Errors::CriteriaArgumentRequired, :near if criterion.nil? and_with_operator(criterion, '$near') end |
# near_sphere (criterion) ⇒可选
将 $nearSphere 标准添加到地理选择中。
432 433 434 435 436 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 432 def near_sphere(criterion) 提高 Errors::CriteriaArgumentRequired, :near_sphere if criterion.nil? and_with_operator(criterion, ' $nearSphere ') end |
#否定? ⇒ true | false
当前的可选操作是否会否定下一个选择?
490 491 492 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 490 def 否定? !!否定 end |
# nin (condition) ⇒ Selectable也称为: not_in
将 $nin 选项添加到可选择项中。
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(条件) 提高 Errors::CriteriaArgumentRequired, :nin if 条件.nil? 条件 = (条件) if 策略 发送(策略, 条件, '$nin') else 条件.注入(克隆) do |查询, (字段, 值)| v = { '$nin' => 值 } v = { ' $not ' => v } if 否定? 查询.add_field_expression(字段.to_s, v) end.reset_strategies! end end |
# none_of (*criteria) ⇒可选
对参数取反,将查询限制为仅查询与参数不匹配的文档。
552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 552 def none_of(*条件) 条件 = _mongoid_flatten_arrays(条件) return dup if 条件.空? 表达式 = 条件.map do |criterion| ( criterion.is_a?(可选) ? criterion.选择器 : criterion ) end self.和(' $nor ' => 表达式) end |
# nor (*criteria) ⇒可选
将 $nor 选择添加到可选择项中。
480 481 482 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 480 def 也不(*条件) _mongoid_add_top_level_operation(' $nor ', 条件) end |
# not (*criteria) ⇒可选
对参数取反,如果未给出参数,则对下一个选择取反。
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 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 509 def not(*条件) if 条件.空? dup.点击 { |查询| 查询.否定 = !查询.否定 } else 条件.compact.注入(克隆) do |C, new_s| new_s = new_s.选择器 if new_s.is_a?(可选) (new_s).每 do |k, v| k = k.to_s if C.选择器[k] || k.start_with?('$') C = C.发送(:__multi__, [ { ' $nor ' => [ { k => v } ] } ], ' $and ') elsif v.is_a?(哈希) C = C.发送(:__multi__, [ { ' $nor ' => [ { k => v } ] } ], ' $and ') else neated_operator = if v.is_a?(regexp) ' $not ' else '$ne' end C = C.发送(:__override__, { k => v }, neated_operator) end end C end end end |
# or (*criteria) ⇒可选
使用 $or 根据接收器中的现有条件和提供的参数创建一个或关系。
此行为(接收器成为析取操作数之一)与 ActiveRecord 的 or 行为匹配。
使用any_of可添加参数的析取,作为接收器中已存在条件的附加约束。
每个参数都可以是哈希、条件对象、哈希或条件对象的大量或嵌套大量。 嵌套数组将被展平,并且可以是任意深度。 传递数组已被弃用。
595 596 597 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 595 def or(*条件) _mongoid_add_top_level_operation(' $or ', 条件) end |
# text_search (terms, opts = nil) ⇒可选
查询操作符目前无法在查询中提供多个文本搜索条件。Mongoid 将构建这样的查询,但服务器在尝试执行该查询时将返回错误。
构建文本Atlas Search选择器。
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 723 def text_search(术语, opts = nil) 提高 Errors::CriteriaArgumentRequired, :terms if 术语.nil? 克隆.点击 do |查询| criterion = { '$text' => { ' $ 搜索 ' => 术语 } } criterion['$text'].合并!(opts) if opts if 查询.选择器['$text'] # 根据 https://www.mongodb.com/zh-cn/docs/manual/reference/operator/query/text/ # 目前不支持多个 $text 表达式 # MongoDB 服务器,但正确构建查询而不是 # 用当前的文本搜索条件覆盖以前的文本搜索条件 # 给定一个。 Mongoid.记录器.WARN('服务器当前不支持每个查询使用多个 $text 表达式') 查询.选择器 = { ' $and ' => [ 查询.选择器 ] }.合并(merge)(criterion) else 查询.选择器 = 查询.选择器.合并(merge)(criterion) end end end |
# where (*criteria) ⇒可选
这是大多数 MongoDB 查询的一般入口点。 这会创建一个标准字段:值选择和使用哈希方法扩展的选择,或者如果提供了string ,则创建 $where 选择。
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 757 def WHERE(*条件) return 克隆.reset_strategies! if 条件.空? 条件.注入(self) do |查询, criterion| 提高 Errors::CriteriaArgumentRequired, :where if criterion.nil? # 我们需要将标准保存在实例变量中,以便 # 可修改的方法知道如何创建多态对象。 # 请注意,该方法原则上接受多个条件, # 但只有第一个会存储在 @criterion 中。 这 # 效果很好,因为 first_or_create 等方法 # 只为 #where 指定一个条件。 @criterion = criterion if criterion.is_a?(字符串) 查询.js_query(criterion) else 查询.expr_query(criterion) end end end |
# with_size (criterion) ⇒可选
该方法被命名为 #with_size,以免与针对可枚举值或符号的任何现有 #size 方法发生冲突。
为数组字段添加 $size 选择。
669 670 671 672 673 674 675 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 669 def with_size(criterion) 提高 Errors::CriteriaArgumentRequired, :with_size if criterion.nil? typed_override(criterion, ' $size ') do |值| ::整型.发展(值) end end |
# with_type (criterion) ⇒可选
http://vurl.me/PGOU包含所有类型的列表。
将 $type 选择添加到可选对象中。
693 694 695 696 697 698 699 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 693 def with_type(criterion) 提高 Errors::CriteriaArgumentRequired, :with_type if criterion.nil? typed_override(criterion, '$type') do |值| ::整型.发展(值) end end |