模块: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 选项。
-
# geo_spatial (criterion) ⇒ 可选
添加 $geoIntersects 或 $geoWithin 选区。
-
# gt (criterion) ⇒ 可选
将 $gt 条件添加到选择器。
-
# gte (criterion) ⇒ 可选
将 $gte 标准添加到选择器。
-
# in (condition) ⇒ 可选择(也可:#any_in)
将 $in 选项添加到可选内容中。
-
# 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包含的方法
实例属性详细信息
#取反">对象
返回属性否定值。
25 26 27 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 25 def 否定 @negating end |
# negating 如果下一个表达式是否定的。 (如果) ⇒对象
25 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 25 attr_accessor :negating, :selector |
#选择器⇒对象
返回属性选择器的值。
25 26 27 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 25 def 选择器 @selector end |
# selector查询选择器。 (查询选择器。) ⇒对象
25 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 25 attr_accessor :negating, :selector |
类方法详细信息
。 Forwardables ⇒ Array<Symbol>
获取可从模型转发到的可选对象上的方法。
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 |
实例方法详细信息
# all (*criteria) ⇒ Selectable也称为: all_in
添加 $all 条件。
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 所有(*条件) if 条件.空? return 克隆.点击 do |查询| 查询.reset_strategies! end end 条件.注入(克隆) do |查询, 条件| if 条件.nil? 提高 Errors::CriteriaArgumentRequired, :all end 条件 = (条件) if 策略 发送(策略, 条件, " $all ") else 条件.注入(查询) do |_query, (字段, 值)| v = {' $all ' => 值} if 否定? v = {' $not ' => v} end _query.add_field_expression(字段.to_s, v) end end end.reset_strategies! end |
# and (*criteria) ⇒ Selectable也称为: all_of
添加 $and 条件。
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 和(*条件) _mongoid_flatten_arrays(条件).注入(self.克隆) do |C, new_s| if new_s.is_a?(可选) new_s = new_s.选择器 end 标准化 = (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?('$') } then 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
使接收者成为析取操作数之一。
每个参数都可以是哈希、条件对象、哈希或条件对象的大量或嵌套大量。 嵌套数组将被展平,并且可以是任意深度。 传递数组已被弃用。
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(*条件) 条件 = _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) ⇒可选
添加范围选择。
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? 提高 Errors::CriteriaArgumentRequired, : between end 选择(criterion) do |选择器, 字段, 值| 选择器.存储( 字段, { " $gte " => 值.min, " $lte " => 值.Max } ) end end |
# elem_match (criterion) ⇒可选
使用 $elemMatch 进行选择。
154 155 156 157 158 159 160 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 154 def elem_match(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :elem_match end and_with_operator(criterion, " $elemMatch ") end |
# eq (criterion) ⇒可选
将 $eq 标准添加到选择器。
257 258 259 260 261 262 263 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 257 def eq(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :eq end and_with_operator(criterion, " $eq ") end |
# exists (criterion) ⇒可选
添加 $exists 选项。
177 178 179 180 181 182 183 184 185 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 177 def 存在(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :exists end typed_override(criterion, " $exists ") do |值| Mongoid::布尔.发展(值) end end |
# geo_spatial (criterion) ⇒可选
$geoIntersects 的唯一有效几何形状是::intersects_line、:intersects_point 和 :intersects_polygon。
$geoWithin 查询的唯一有效选项是几何形状 :within_polygon 和运算符 :within_box。
$geoWithin查询的 :within_box操作符期望左下(西南)坐标对作为第一个参数,右上角(东北)坐标对作为第二个参数。 重要提示:传递纬度和经度时,经度应为坐标对的第一个元素。 来源: 操作符
添加 $geoIntersects 或 $geoWithin 选区。 必须按示例所示使用符号操作符来扩展条件。
224 225 226 227 228 229 230 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 224 def geo_spatial(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :geo_spatial end __merge__(criterion) end |
# gt (criterion) ⇒可选
将 $gt 条件添加到选择器。
277 278 279 280 281 282 283 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 277 def gt(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :gt end and_with_operator(criterion, " $gt ") end |
# gte (criterion) ⇒可选
将 $gte 标准添加到选择器。
297 298 299 300 301 302 303 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 297 def gte(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :gte end and_with_operator(criterion, " $gte ") end |
# in (condition) ⇒ Selectable也称为: any_in
将 $in 选项添加到可选内容中。
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(条件) if 条件.nil? 提高 Errors::CriteriaArgumentRequired, :in end 条件 = (条件) if 策略 发送(策略, 条件, " $in ") else 条件.注入(克隆) do |查询, (字段, 值)| v = {' $in ' => 值} if 否定? v = {' $not ' => v} end 查询.add_field_expression(字段.to_s, v) end.reset_strategies! end end |
# lt (criterion) ⇒可选
将 $lt 条件添加到选择器。
353 354 355 356 357 358 359 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 353 def lt(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :lt end and_with_operator(criterion, " $lt ") end |
# lte (criterion) ⇒可选
将 $lte 标准添加到选择器。
373 374 375 376 377 378 379 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 373 def lte(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :lte end and_with_operator(criterion, " $lte ") end |
# max_distance (criterion) ⇒可选
将 $maxDistance 选择添加到可选对象中。
390 391 392 393 394 395 396 397 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 390 def max_distance(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :max_distance end # $maxDistance 必须与 $near 一起给出 __add__(criterion, " $maxDistance ") end |
# mod (criterion) ⇒可选
将 $mod 选择添加到可选内容中。
410 411 412 413 414 415 416 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 410 def mod(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :mod end and_with_operator(criterion, " $mod ") end |
# ne (criterion) ⇒ Selectable也称为:排除
将 $ne 选择添加到可选择项中。
430 431 432 433 434 435 436 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 430 def ne(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :ne end and_with_operator(criterion, "$ne") end |
# near (criterion) ⇒可选
将 $near 标准添加到地理选择中。
451 452 453 454 455 456 457 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 451 def 接近(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :near end and_with_operator(criterion, " $near ") end |
# near_sphere (criterion) ⇒可选
将 $nearSphere 标准添加到地理选择中。
471 472 473 474 475 476 477 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 471 def near_sphere(criterion) if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :near_sphere end and_with_operator(criterion, " $nearSphere ") end |
#否定? ⇒ true | false
当前的可选操作是否会否定下一个选择?
535 536 537 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 535 def 否定? !!否定 end |
# nin (condition) ⇒ Selectable也称为: not_in
将 $nin 选项添加到可选择项中。
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(条件) if 条件.nil? 提高 Errors::CriteriaArgumentRequired, :nin end 条件 = (条件) if 策略 发送(策略, 条件, " $nin ") else 条件.注入(克隆) do |查询, (字段, 值)| v = {'$nin' => 值} if 否定? v = {' $not ' => v} end 查询.add_field_expression(字段.to_s, v) end.reset_strategies! end end |
# none_of (*criteria) ⇒可选
对参数取反,将查询限制为仅查询与参数不匹配的文档。
601 602 603 604 605 606 607 608 609 610 611 612 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 601 def none_of(*条件) 条件 = _mongoid_flatten_arrays(条件) return dup if 条件.空? 表达式 = 条件.map do |criterion| ( criterion.is_a?(可选) ? criterion.选择器 : criterion) end self.和(' $nor ' => 表达式) end |
# nor (*criteria) ⇒可选
将 $nor 选择添加到可选择项中。
525 526 527 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 525 def 也不(*条件) _mongoid_add_top_level_operation(' $nor ', 条件) end |
# not (*criteria) ⇒可选
对参数取反,如果未给出参数,则对下一个选择取反。
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(*条件) if 条件.空? dup.点击 { |查询| 查询.否定 = !查询.否定 } else 条件.compact.注入(self.克隆) do |C, new_s| if new_s.is_a?(可选) new_s = new_s.选择器 end (new_s).每 do |k, v| k = k.to_s if C.选择器[k] || k.start_with?('$') C = C.发送(:__multi__, [{' $nor ' => [{k => v}]}], ' $and ') else if v.is_a?(哈希) C = C.发送(:__multi__, [{' $nor ' => [{k => v}]}], ' $and ') else if v.is_a?(regexp) neated_operator = ' $not ' else neated_operator = '$ne' end C = C.发送(:__override__, {k => v}, neated_operator) end end end C end end end |
# or (*criteria) ⇒可选
使用 $or 根据接收器中的现有条件和提供的参数创建一个或关系。
此行为(接收器成为析取操作数之一)与 ActiveRecord 的or
行为匹配。
使用any_of
可添加参数的析取,作为接收器中已存在条件的附加约束。
每个参数都可以是哈希、条件对象、哈希或条件对象的大量或嵌套大量。 嵌套数组将被展平,并且可以是任意深度。 传递数组已被弃用。
644 645 646 |
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 644 def or(*条件) _mongoid_add_top_level_operation(' $or ', 条件) end |
# text_search (terms, opts = nil) ⇒可选
根据www.mongodb.com/zh-cn/docs/manual/reference/operator/query/text / 目前无法在查询中提供多个文本Atlas Search条件。 Mongoid 将构建这样的查询,但服务器在尝试执行该查询时将返回错误。
构建文本Atlas Search选择器。
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(术语, opts = nil) if 术语.nil? 提高 Errors::CriteriaArgumentRequired, :terms end 克隆.点击 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 选择。
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(*条件) 条件.注入(克隆) do |查询, criterion| if criterion.nil? 提高 Errors::CriteriaArgumentRequired, :where end # 我们需要将标准保存在实例变量中,以便 # 可修改的方法知道如何创建多态对象。 # 请注意,该方法原则上接受多个条件, # 但只有第一个会存储在 @criterion 中。 这 # 效果很好,因为 first_or_create 等方法 # 只为 #where 指定一个条件。 @criterion = criterion if criterion.is_a?(字符串) js_query(criterion) else expr_query(criterion) end end.reset_strategies! end |
# with_size (criterion) ⇒可选
该方法被命名为 #with_size,以免与针对可枚举值或符号的任何现有 #size 方法发生冲突。
为数组字段添加 $size 选择。
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? 提高 Errors::CriteriaArgumentRequired, :with_size end typed_override(criterion, " $size ") do |值| ::整型.发展(值) end end |
# with_type (criterion) ⇒可选
vurl.me/PGOU包含所有类型的列表。
将 $type 选择添加到可选对象中。
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? 提高 Errors::CriteriaArgumentRequired, :with_type end typed_override(criterion, " $type ") do |值| ::整型.发展(值) end end |