模块: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

实例属性详细信息

#取反">对象

返回属性否定值。



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

类方法详细信息

ForwardablesArray<Symbol>

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

例子:

获取可转发方法。

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

例子:

添加条件。

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

在 where 查询中执行 $all。

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

参数:

  • *criteria (哈希... )

    $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

    条件 = expand_condition_to_array_values(条件)

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

例子:

添加条件。

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

参数:

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

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

返回:

  • (可选)

    新的可选。



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

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

例子:

添加 $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 对象或其数组。 传递数组已被弃用。

返回:

  • (可选)

    新的可选。



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?(可选)
        _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 (哈希)

    多个键/范围对。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

为单个匹配项添加条件。

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 (哈希)

    字段/匹配对。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

添加 $eq 标准。

selectable.eq(age: 60)

在 where 查询中执行 $eq。

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

参数:

  • criterion (哈希)

    要检查的字段/值对。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

添加单个选择。

selectable.exists(field: true)

添加多个选择。

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

在 where 查询中执行 $exists。

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

参数:

  • criterion (哈希)

    字段/boolean 检查存在性。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

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

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 (哈希)

    标准。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

添加 $gt 标准。

selectable.gt(age: 60)

在 where查询中执行 $gt。

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

参数:

  • criterion (哈希)

    要检查的字段/值对。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

添加 $gte 标准。

selectable.gte(age: 60)

在 where查询中执行 $gte。

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

参数:

  • criterion (哈希)

    要检查的字段/值对。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

在数组上添加 $in 选择。

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

在范围上添加 $in 选择。

selectable.in(age: 18..24)

在 where 查询中执行 $in。

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

参数:

  • 条件 (哈希)

    字段/值条件对。

返回:

  • (可选)

    克隆的可选内容。



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

  条件 = expand_condition_to_array_values(条件)

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

例子:

添加 $lt 标准。

selectable.lt(age: 60)

在 where查询中执行 $lt。

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

参数:

  • criterion (哈希)

    要检查的字段/值对。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

添加 $lte 标准。

selectable.lte(age: 60)

在 where查询中执行 $lte。

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

参数:

  • criterion (哈希)

    要检查的字段/值对。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

添加 $maxDistance 选择。

selectable.max_distance(location: 10)

参数:

  • criterion (哈希)

    字段/距离对。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

添加 $mod 选项。

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

在 where 查询中执行 $mod。

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

参数:

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

例子:

查询某物的值 $ne。

selectable.ne(field: 10)

在 where查询中执行 $ne。

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

参数:

  • criterion (哈希)

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

例子:

添加 $near 选择。

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

在 where 查询中执行 $near。

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

参数:

  • criterion (哈希)

    字段/位置对。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

添加 $nearSphere 选择。

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

在 where 查询中执行 $nearSphere。

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

参数:

  • criterion (哈希)

    字段/位置对。

返回:

  • (可选)

    克隆的可选内容。



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

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

例子:

是可选的否定吗?

selectable.negating?

返回:

  • ( true | false )

    如果可选项是否定的。



535
536
537
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 535

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 ])

参数:

  • 条件 (哈希)

    字段/值条件对。

返回:

  • (可选)

    克隆的可选内容。



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

  条件 = expand_condition_to_array_values(条件)

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

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

例子:

排除单个条件。

selectable.none_of(name: /Bob/)

排除多个条件。

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

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

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

参数:

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

    键/值对匹配或要否定的 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|
    _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 对象。

返回:

  • (可选)

    新的可选。



525
526
527
# File 'lib/mongoid/criteria/queryable/selectable.rb', line 525

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 对象。

返回:

  • (可选)

    新的可选。



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
      _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 ')
        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可添加参数的析取,作为接收器中已存在条件的附加约束。

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

例子:

添加 $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 对象或其数组。 传递数组已被弃用。

返回:

  • (可选)

    新的可选。



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

例子:

构建文本Atlas Search选择器。

selectable.text_search("testing")

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

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

参数:

  • 术语 ( string | Symbol )

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

  • opts 哈希 (默认为: nil

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

返回:

  • (可选)

    克隆的可选内容。



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

例子:

添加标准选择。

selectable.where(name: "syd")

添加 JavaScript 选择。

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

参数:

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

    标准选择或JavaScript string 。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

添加 $size 选择。

selectable.with_size(field: 5)

在 where 查询中执行 $size。

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

参数:

  • criterion (哈希)

    字段/大小对标准。

返回:

  • (可选)

    克隆的可选内容。



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

例子:

添加 $type 选择。

selectable.with_type(field: 15)

在 where 查询中执行 $type。

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

参数:

  • criterion (哈希)

    字段/类型对。

返回:

  • (可选)

    克隆的可选内容。



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