模块: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 常量。

'多边形'

实例属性摘要折叠

类方法摘要折叠

实例方法摘要折叠

Macroable包含的方法

key

实例属性详细信息

#取反">对象

返回属性否定值。



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

类方法详细信息

ForwardablesArray<Symbol>

获取可从模型转发到的可选对象上的方法。

例子:

获取可转发方法。

Selectable.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 条件。

例子:

添加条件。

selectable.all(field: [ 1, 2 ])

在 where 查询中执行 $all。

selectable.where(:field.all => [ 1, 2 ])

参数:

  • *criteria (哈希... )

    $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?

    条件 = expand_condition_to_array_values(条件)

    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 条件。

例子:

添加条件。

selectable.and({ field: value }, { other: value })

参数:

  • *criteria ( [ Hash | Criteria | Array<Hash | Criteria > ]... )

    多个键/值对匹配项或全部匹配才能返回结果的 Criteria 对象。

返回:

  • (可选)

    新的可选。



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?(可选)
    标准化 = _mongoid_expand_keys(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使接收者成为析取操作数之一。

每个参数都可以是哈希、条件对象、哈希或条件对象的大量或嵌套大量。 嵌套数组将被展平,并且可以是任意深度。 传递数组已被弃用。

例子:

添加 $or 选项,其中两个字段都必须具有指定值。

selectable.any_of(field: 1, field: 2)

在任一值匹配便已足够的位置添加 $or 选择。

selectable.any_of({field: 1}, {field: 2})

与上一个示例相同,但使用已弃用的大量换行。

selectable.any_of([{field: 1}, {field: 2}])

与上一个示例相同,但也已弃用。

selectable.any_of([{field: 1}], [{field: 2}])

参数:

  • *criteria ( [ Hash | Criteria | Array<Hash | Criteria > ]... )

    多个键/值对匹配或 Criteria 对象或其数组。 传递数组已被弃用。

返回:

  • (可选)

    新的可选。



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?(可选)
        _mongoid_expand_keys(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) ⇒可选

添加范围选择。

例子:

匹配单个范围内的结果。

selectable.between(field: 1..2)

匹配多个范围之间的结果。

selectable.between(field: 1..2, other: 5..7)

参数:

  • 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 进行选择。

例子:

为单个匹配项添加条件。

selectable.elem_match(field: { name: "value" })

为多个匹配项添加条件。

selectable.elem_match(
  field: { name: "value" },
  other: { name: "value"}
)

在 where 查询中执行 $elemMatch。

selectable.where(:field.elem_match => { name: "value" })

参数:

  • criterion (哈希)

    字段/匹配对。

返回:

  • (可选)

    克隆的可选内容。

引发:



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 标准添加到选择器。

例子:

添加 $eq 标准。

selectable.eq(age: 60)

在 where 查询中执行 $eq。

selectable.where(:field.eq => 10)

参数:

  • criterion (哈希)

    要检查的字段/值对。

返回:

  • (可选)

    克隆的可选内容。

引发:



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 选项。

例子:

添加单个选择。

selectable.exists(field: true)

添加多个选择。

selectable.exists(field: true, other: false)

在 where 查询中执行 $exists。

selectable.where(:field.exists => true)

参数:

  • criterion (哈希)

    字段/boolean 检查存在性。

返回:

  • (可选)

    克隆的可选内容。

引发:



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

字段名称和操作符可以字符串或符号的形式给出。

例子:

创建选区。

selectable.expr_query(age: 50)

参数:

  • criterion (哈希)

    字段/值对。

返回:

  • (可选)

    克隆的可选内容。

引发:

  • ( ArgumentError )


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

  标准化 = _mongoid_expand_keys(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 选区。 必须按示例所示使用符号操作符来扩展条件。

例子:

为直线添加地理相交条件。

query.geo_spatial(:location.intersects_line => [[ 1, 10 ], [ 2, 10 ]])

为点添加地理相交条件。

query.geo_spatial(:location.intersects_point => [[ 1, 10 ]])

为多边形添加地理相交条件。

query.geo_spatial(:location.intersects_polygon => [[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]])

为多边形添加地理范围内的标准。

query.geo_spatial(:location.within_polygon => [[ 1, 10 ], [ 2, 10 ], [ 1, 10 ]])

为方框添加地理范围内条件。

query.geo_spatial(:location.within_box => [[ 1, 10 ], [ 2, 10 ])

参数:

  • criterion (哈希)

    标准。

返回:

  • (可选)

    克隆的可选内容。

引发:



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 条件添加到选择器。

例子:

添加 $gt 标准。

selectable.gt(age: 60)

在 where查询中执行 $gt。

selectable.where(:field.gt => 10)

参数:

  • criterion (哈希)

    要检查的字段/值对。

返回:

  • (可选)

    克隆的可选内容。

引发:



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 标准添加到选择器。

例子:

添加 $gte 标准。

selectable.gte(age: 60)

在 where查询中执行 $gte。

selectable.where(:field.gte => 10)

参数:

  • criterion (哈希)

    要检查的字段/值对。

返回:

  • (可选)

    克隆的可选内容。

引发:



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 选项添加到可选内容中。

例子:

在数组上添加 $in 选择。

selectable.in(age: [ 1, 2, 3 ])

在范围上添加 $in 选择。

selectable.in(age: 18..24)

在 where 查询中执行 $in。

selectable.where(:field.in => [ 1, 2, 3 ])

参数:

  • 条件 (哈希)

    字段/值条件对。

返回:

  • (可选)

    克隆的可选内容。

引发:



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?

  条件 = expand_condition_to_array_values(条件)

  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 选区。

例子:

创建 JavaScript 选择。

selectable.js_query("this.age == 50")

参数:

  • criterion ( string )

    作为 string 的 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 条件添加到选择器。

例子:

添加 $lt 标准。

selectable.lt(age: 60)

在 where查询中执行 $lt。

selectable.where(:field.lt => 10)

参数:

  • criterion (哈希)

    要检查的字段/值对。

返回:

  • (可选)

    克隆的可选内容。

引发:



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 标准添加到选择器。

例子:

添加 $lte 标准。

selectable.lte(age: 60)

在 where查询中执行 $lte。

selectable.where(:field.lte => 10)

参数:

  • criterion (哈希)

    要检查的字段/值对。

返回:

  • (可选)

    克隆的可选内容。

引发:



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 选择添加到可选对象中。

例子:

添加 $maxDistance 选择。

selectable.max_distance(location: 10)

参数:

  • criterion (哈希)

    字段/距离对。

返回:

  • (可选)

    克隆的可选内容。

引发:



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 选择添加到可选内容中。

例子:

添加 $mod 选项。

selectable.mod(field: [ 10, 1 ])

在 where 查询中执行 $mod。

selectable.where(:field.mod => [ 10, 1 ])

参数:

  • 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 选择添加到可选择项中。

例子:

查询某物的值 $ne。

selectable.ne(field: 10)

在 where查询中执行 $ne。

selectable.where(:field.ne => "value")

参数:

  • criterion (哈希)

    字段/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 标准添加到地理选择中。

例子:

添加 $near 选择。

selectable.near(location: [ 23.1, 12.1 ])

在 where 查询中执行 $near。

selectable.where(:field.near => [ 23.2, 12.1 ])

参数:

  • criterion (哈希)

    字段/位置对。

返回:

  • (可选)

    克隆的可选内容。

引发:



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 标准添加到地理选择中。

例子:

添加 $nearSphere 选择。

selectable.near_sphere(location: [ 23.1, 12.1 ])

在 where 查询中执行 $nearSphere。

selectable.where(:field.near_sphere => [ 10.11, 3.22 ])

参数:

  • criterion (哈希)

    字段/位置对。

返回:

  • (可选)

    克隆的可选内容。

引发:



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

当前的可选操作是否会否定下一个选择?

例子:

是可选的否定吗?

selectable.negating?

返回:

  • ( true | false )

    如果可选项是否定的。



490
491
492
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 490

def 否定?
  !!否定
end

# nin (condition) ⇒ Selectable也称为: not_in

将 $nin 选项添加到可选择项中。

例子:

在大量上添加 $nin 选择。

selectable.nin(age: [ 1, 2, 3 ])

在范围上添加 $nin 选择。

selectable.nin(age: 18..24)

在 where 查询中执行 $nin。

selectable.where(:field.nin => [ 1, 2, 3 ])

参数:

  • 条件 (哈希)

    字段/值条件对。

返回:

  • (可选)

    克隆的可选内容。

引发:



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?

  条件 = expand_condition_to_array_values(条件)

  if 策略
    发送(策略, 条件, '$nin')
  else
    条件.注入(克隆) do |查询, (字段, )|
      v = { '$nin' =>  }
      v = { ' $not ' => v } if 否定?
      查询.add_field_expression(字段.to_s, v)
    end.reset_strategies!
  end
end

# none_of (*criteria) ⇒可选

对参数取反,将查询限制为仅查询与参数不匹配的文档。

例子:

排除单个条件。

selectable.none_of(name: /Bob/)

排除多个条件。

selectable.none_of(name: /Bob/, country: "USA")

将多个条件作为数组排除。

selectable.none_of([{ name: /Bob/ }, { country: "USA" }])

参数:

  • *criteria ( [ 哈希 |条件]... )

    键/值对匹配或要否定的 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|
    _mongoid_expand_keys(
      criterion.is_a?(可选) ? criterion.选择器 : criterion
    )
  end

  self.(' $nor ' => 表达式)
end

# nor (*criteria) ⇒可选

将 $nor 选择添加到可选择项中。

例子:

添加 $nor 选项。

selectable.nor(field: 1, field: 2)

参数:

  • *criteria ( [ Hash | Criteria | Array<Hash | Criteria > ]... )

    多个键/值对匹配项或 Criteria 对象。

返回:

  • (可选)

    新的可选。



480
481
482
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 480

def 也不(*条件)
  _mongoid_add_top_level_operation(' $nor ', 条件)
end

# not (*criteria) ⇒可选

对参数取反,如果未给出参数,则对下一个选择取反。

例子:

否定下一个选择。

selectable.not.in(field: [ 1, 2 ])

添加 $not 条件。

selectable.not(name: /Bob/)

在 where查询中执行 $not。

selectable.where(:field.not => /Bob/)

参数:

  • *criteria ( [ 哈希 |条件]... )

    键/值对匹配或要否定的 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?(可选)
      _mongoid_expand_keys(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可添加参数的析取,作为接收器中已存在条件的附加约束。

每个参数都可以是哈希、条件对象、哈希或条件对象的大量或嵌套大量。 嵌套数组将被展平,并且可以是任意深度。 传递数组已被弃用。

例子:

添加 $or 选项,其中两个字段都必须具有指定值。

selectable.or(field: 1, field: 2)

在任一值匹配便已足够的位置添加 $or 选择。

selectable.or({field: 1}, {field: 2})

与上一个示例相同,但使用已弃用的大量换行。

selectable.or([{field: 1}, {field: 2}])

与上一个示例相同,但也已弃用。

selectable.or([{field: 1}], [{field: 2}])

参数:

  • *criteria ( [ Hash | Criteria | Array<Hash | Criteria > ]... )

    多个键/值对匹配或 Criteria 对象或其数组。 传递数组已被弃用。

返回:

  • (可选)

    新的可选。



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选择器。

例子:

构建文本Atlas Search选择器。

selectable.text_search("testing")

使用选项构建文本搜索选择器。

selectable.text_search("testing", :$language => "fr")

参数:

  • 术语 ( string | Symbol )

    MongoDB 解析并用于查询文本索引的词语字符串。

  • opts 哈希 (默认为: nil

    文本Atlas Search选项。 有关选项,请参阅 MongoDB 文档。

返回:

  • (可选)

    克隆的可选内容。

引发:



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 选择。

例子:

添加标准选择。

selectable.where(name: "syd")

添加 JavaScript 选择。

selectable.where("this.name == 'syd'")

参数:

  • *criterion ( [ Hash | string ]... )

    标准选择或JavaScript string 。

返回:

  • (可选)

    克隆的可选内容。



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 选择。

例子:

添加 $size 选择。

selectable.with_size(field: 5)

在 where 查询中执行 $size。

selectable.where(:field.with_size => 10)

参数:

  • criterion (哈希)

    字段/大小对标准。

返回:

  • (可选)

    克隆的可选内容。

引发:



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 选择添加到可选对象中。

例子:

添加 $type 选择。

selectable.with_type(field: 15)

在 where 查询中执行 $type。

selectable.where(:field.with_type => 15)

参数:

  • criterion (哈希)

    字段/类型对。

返回:

  • (可选)

    克隆的可选内容。

引发:



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