类:Mongoid::Contextual::Mongo

继承:
对象
  • 对象
显示全部
扩展方式:
可转发
包括:
Enumerable、 Association::EagerLoadableAtomicAggregable::MongoQueryable
定义于:
lib/mongoid/contextual/ mongo .rb ,
lib/mongoid/contextual/ mongo /documents_loader.rb

Overview

上下文对象,用于对保存在数据库中但尚未加载到应用程序内存中的文档执行批量查询和持久化操作。

在命名空间下定义

类: DocumentsLoader

常量摘要折叠

OPTIONS =

选项常量。

[ :hint,
  :limit,
  :skip,
  :sort,
  :batch_size,
  :max_scan,
  :max_time_ms,
  :快照,
  :comment,
  :read,
  :cursor_type,
  :collation
].冻结

Atomic中包含的常量

Atomic::UPDATES

实例属性摘要折叠

Queryable中包含的属性

#collection#collection 要查询的集合。#criteria#criteria 上下文的条件。#klass#klass 条件的 klass。

实例方法摘要折叠

Queryable中包含的方法

#blank?

Association::EagerLoadable 中包含的方法

#eager_load#eager_loadable? , #preload

Atomic中包含的方法

#add_atomic_pull , #add_atomic_unset , #atomic_array_add_to_sets , #atomic_array_pulls , #atomic_array_pushes , #atomic_attribute_name , #atomic_delete_modifier , #atomic_insert_modifier , #atomic_path , #atomic_paths , #atomic_position , #atomic_pulls , #atomic_pushes , #atomic_sets , #atomic_unsets , #atomic_updates , #delayed_atomic_pulls#delayed_atomic_sets#delayed_atomic_unsets#flag_as_destroyed#flag_destroys#process_flag_destroys

Aggregable::Mongo中包含的方法

#aggregates#avg#max#min#sum

构造函数详情

#initialize (criteria) ⇒ mongo

创建新的mongo上下文。 这会将操作委托给底层驱动程序。

例子:

创建新上下文。

Mongo.new(criteria)

参数:



270
271
272
273
274
275
276
# File 'lib/mongoid/contextual/ Mongo.rb', line 270

def 初始化(条件)
  @criteria, @klass = 条件, 条件.klass
  @collection = @klass.集合
  条件.发送(:merge_type_selection)
  @view = 集合.find(条件.选择器, 会话: _session)
  apply_options
end

实例属性详细信息

# documents_loader对象(只读)

返回属性 documents_loader 的值。



54
55
56
# File 'lib/mongoid/contextual/ Mongo.rb', line 54

def documents_loader
  @documents_loader
end

#视图哈希(只读)

对条件运行解释。

例子:

解释标准。

Band.where(name: "Depeche Mode").explain

参数:

  • 选项 (哈希)

    可自定义选项(请参阅 Mongo::Collection::View::Explainable)

返回:

  • (哈希)

    解释结果。



42
43
44
# File 'lib/mongoid/contextual/ Mongo.rb', line 42

def 查看
  @view
end

# view mongo集合视图。 (TheMongocollectionview。) ⇒对象(只读)



42
# File 'lib/mongoid/contextual/ Mongo.rb', line 42

attr_reader :view

实例方法详细信息

# count (options = {}, &block) ⇒ Integer

获取与查询匹配的文档数。

例子:

获取匹配文档的数量。

context.count

使用提供的选项获取文档计数。

context.count(limit: 1)

获取所提供区块为 true 的计数。

context.count do |doc|
  doc.likes > 1
end

参数:

  • 选项 哈希 (默认为: {}

    要计入计数的选项,例如 skip 和 limit。

返回:

  • ( Integer )

    匹配项的数量。



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mongoid/contextual/ Mongo.rb', line 73

def 数数(选项 = {}, )
  return () if block_given?

  if valid_for_count_documents?
    查看.count_documents(选项)
  else
    # TODO: 在删除已弃用的 for_js API时删除此项。
    # https://jira.mongodb.org/browse/MONGOID- 5681
    查看.数数(选项)
  end
end

# deletenil也称为: delete_all

删除数据库中与选择器匹配的所有文档。

例子:

删除所有文档。

context.delete

返回:

  • ( nil )

    Nil.



114
115
116
# File 'lib/mongoid/contextual/ Mongo.rb', line 114

def 删除
  查看.delete_many.Deleted_Count
end

#destroynil也称为: destroy_all

销毁数据库中与选择器匹配的所有文档。

例子:

销毁所有文档。

context.destroy

返回:

  • ( nil )

    Nil.



125
126
127
128
129
130
131
# File 'lib/mongoid/contextual/ Mongo.rb', line 125

def 销毁
  .注入(0) do |数数, doc|
    doc.销毁
    数数 += 1 if 已确认_写入?
    数数
  end
end

# distinct (field) ⇒ Array<Object>

获取数据库中所提供字段的非重复值。

例子:

获取不同的值。

context.distinct(:name)

参数:

  • 字段 ( string | Symbol )

    字段的名称。

返回:

  • ( Array<Object> )

    字段的非重复值。



142
143
144
145
146
147
148
149
# File 'lib/mongoid/contextual/ Mongo.rb', line 142

def distinct(字段)
  名称 = klass.clean_localized_field_names(字段)

  查看.distinct(名称).map do ||
    is_translation = " #{ name } _translations " == 字段.to_s
    recursive_demongoize(名称, , is_translation)
  end
end

#each (&block) ⇒枚举器

遍历上下文。 如果提供了区块,则为每个区块让出一个 Mongoid文档,否则返回一个枚举。

例子:

遍历上下文。

context.each do |doc|
  puts doc.name
end

返回:

  • (枚举器)

    枚举器。



160
161
162
163
164
165
166
167
168
169
# File 'lib/mongoid/contextual/ Mongo.rb', line 160

def ()
  if block_given?
    documents_for_iteration. do |doc|
      Yield_Document(doc, )
    end
    self
  else
    to_enum
  end
end

#estimated_count (options = {}) ⇒ Integer

获取与查询匹配的文档的估计数量。

与 count 不同,estimated_count 不占用区块,因为它传统上不像 count 那样在 Enumarable 上定义(使用区块)。

例子:

获取匹配文档的估计数量。

context.estimated_count

参数:

  • 选项 哈希 (默认为: {}

    要考虑到计数的选项,例如 maxTimeMS。

返回:

  • ( Integer )

    匹配项的数量。



97
98
99
100
101
102
103
104
105
106
# File 'lib/mongoid/contextual/ Mongo.rb', line 97

def estimated_count(选项 = {})
  除非 self.条件.选择器.空?
    if klass.default_scoping?
      提高 Mongoid::Errors::InvalidEstimatedCountScoping.new(self.klass)
    else
      提高 Mongoid::Errors::InvalidEstimatedCountCriteria.new(self.klass)
    end
  end
  查看.estimated_document_count(选项)
end

#是否存在? (id_or_conditions = :none) ⇒ true | false

注意:

我们在这里不使用计数,因为mongo不使用计数的 B-Tree 索引。

该上下文是否存在任何文档。

例子:

该上下文是否存在任何文档。

context.exists?

给定 _id 是否存在任何文档。

context.exists?(BSON::ObjectId(...))

在给定条件下是否存在任何文档。

context.exists?(name: "...")

参数:

  • id_or_conditions 哈希 | 对象 | false (默认为: :none

    要搜索的_id 、条件哈希、nil 或 false。

返回:

  • ( true | false )

    如果计数大于零。 如果传递 nil 或 false,则始终为 false。



190
191
192
193
194
195
196
197
198
# File 'lib/mongoid/contextual/ Mongo.rb', line 190

def 存在吗?(id_or_conditions = : none)
  return false if self.查看.limit == 0
  案例 id_or_conditions
  when : none then !!(查看.投影(_id: 1).limit(1).first)
  when nil, false then false
  when 哈希 then mongo.new(条件.WHERE(id_or_conditions)).存在吗?
  else mongo.new(条件.WHERE(_id: id_or_conditions)).存在吗?
  end
end

#第五个文档| nil

获取数据库中条件选择器的第五个文档。

例子:

获取第五个文档。

context.fifth

返回:

  • (Document | nil)

    第五个文档;如果未找到,则为 nil。



699
700
701
# File 'lib/mongoid/contextual/ Mongo.rb', line 699

def 第五
  retrieve_nth(4)
end

#第五!文档

获取数据库中条件选择器的第五个文档,如果未找到,则引发错误。

例子:

获取第五个文档。

context.fifth!

返回:

  • (文档)

    第五个文档。

引发:



713
714
715
# File 'lib/mongoid/contextual/ Mongo.rb', line 713

def 第五!
  第五 || Raise_document_not_Found_error
end

# find_first对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

返回第一个结果而不应用排序



256
257
258
259
260
261
# File 'lib/mongoid/contextual/ Mongo.rb', line 256

def find_first
  if raw_doc = 查看.first
    doc = 工厂.from_db(klass, raw_doc, 条件)
    ager_load([doc]).first
  end
end

# find_one_and_delete文档

执行查找和修改命令,用于 MongoDB 的 $findAndModify。 这将删除找到的文档。

例子:

执行命令。

context.find_one_and_delete

返回:

  • (文档)

    命令的结果。



247
248
249
250
251
# File 'lib/mongoid/contextual/ Mongo.rb', line 247

def find_one_and_delete
  if doc = 查看.find_one_and_delete
    工厂.from_db(klass, doc)
  end
end

# find_one_and_replace (replacement, options = {}) ⇒文档

执行查找和修改命令,用于 MongoDB 的 $findAndModify。

例子:

执行命令。

context.find_one_and_update({ likes: 1 })

参数:

  • 替换 (哈希)

    替换。

  • 选项 哈希 (默认为: {}

    命令选项。

选项哈希 ( options ):

  • :return_document ( :before | :after )

    返回 update 之前或之后更新的文档。

  • :更新或插入(upsert) ( true | false )

    如果文档不存在,则创建该文档。

返回:

  • (文档)

    命令的结果。



234
235
236
237
238
# File 'lib/mongoid/contextual/ Mongo.rb', line 234

def find_one_and_replace(替换, 选项 = {})
  if doc = 查看.find_one_and_replace(替换, 选项)
    工厂.from_db(klass, doc)
  end
end

#find_one_and_update(update, options = {}) ⇒ Document

执行查找和修改命令,用于 MongoDB 的 $findAndModify。

例子:

执行命令。

context.find_one_and_update({ "$inc" => { likes: 1 }})

参数:

  • update (哈希)

    更新。

  • 选项 哈希 (默认为: {}

    命令选项。

选项哈希 ( options ):

  • :return_document ( :before | :after )

    返回 update 之前或之后更新的文档。

  • :更新或插入(upsert) ( true | false )

    如果文档不存在,则创建该文档。

返回:

  • (文档)

    命令的结果。



214
215
216
217
218
# File 'lib/mongoid/contextual/ Mongo.rb', line 214

def find_one_and_update(update, 选项 = {})
  if doc = 查看.find_one_and_update(update, 选项)
    工厂.from_db(klass, doc)
  end
end

# first (limit = nil) ⇒文档| nil也称为: one

注意:

在没有对条件定义其他排序的情况下自动对_id添加排序,可能会导致性能问题。 如果在使用 #first 或 #last 时遇到意外的性能下降,并且没有对条件定义排序,请改用 #take。 请注意,#take 不保证顺序。

获取数据库中符合条件的选择器的第一个文档。

例子:

获取第一个文档。

context.first

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (Document | nil)

    第一个文档;如果未找到,则为 nil。



550
551
552
553
554
555
556
# File 'lib/mongoid/contextual/ Mongo.rb', line 550

def first(limit = nil)
  if limit.nil?
    retrieve_nth(0)
  else
    retrieve_nth_with_limit(0, limit)
  end
end

# first!文档

注意:

在没有对条件定义其他排序的情况下自动对_id添加排序,可能会导致性能问题。 如果您在使用 #first! 时遇到意外的性能下降情况 或#last!并且没有对条件定义排序,请使用 #take! 相反。 请注意,#take! 不保证顺序。

获取数据库中条件选择器的第一个文档,如果未找到任何文档,则引发错误。

例子:

获取第一个文档。

context.first!

返回:

  • (文档)

    第一个文档。

引发:



575
576
577
# File 'lib/mongoid/contextual/ Mongo.rb', line 575

def 首先!
  first || Raise_document_not_Found_error
end

#fourth文档| nil

获取数据库中条件选择器的第四个文档。

例子:

获取第四个文档。

context.fourth

返回:

  • (Document | nil)

    第四个文档;如果未找到,则为 nil。



675
676
677
# File 'lib/mongoid/contextual/ Mongo.rb', line 675

def 第四
  retrieve_nth(3)
end

#fourth!文档

获取数据库中条件选择器的第四个文档,如果未找到,则引发错误。

例子:

获取第四个文档。

context.fourth!

返回:

  • (文档)

    第四个文档。

引发:



689
690
691
# File 'lib/mongoid/contextual/ Mongo.rb', line 689

def 第四!
  第四 || Raise_document_not_Found_error
end

#last(limit = nil) ⇒ Document | nil

注意:

在没有对条件定义其他排序的情况下自动对_id添加排序,可能会导致性能问题。 如果在使用 #first 或 #last 时遇到意外的性能下降,并且没有对条件定义排序,请改用 #take。 请注意,#take 不保证顺序。

获取数据库中符合条件的选择器的最后一个文档。

例子:

获取最后一个文档。

context.last

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (Document | nil)

    最后一个文档;如果未找到,则为 nil。



593
594
595
596
597
598
599
# File 'lib/mongoid/contextual/ Mongo.rb', line 593

def last(limit = nil)
  if limit.nil?
    retrieve_nth_to_last(0)
  else
    retrieve_nth_to_last_with_limit(0, limit)
  end
end

# last!文档

注意:

在没有对条件定义其他排序的情况下自动对_id添加排序,可能会导致性能问题。 如果您在使用 #first! 时遇到意外的性能下降情况 或#last!并且没有对条件定义排序,请使用 #take! 相反。 请注意,#take! 不保证顺序。

获取数据库中条件选择器的最后一个文档,如果未找到任何文档,则引发错误。

例子:

获取最后一个文档。

context.last!

返回:

  • (文档)

    最后一个文档。

引发:



617
618
619
# File 'lib/mongoid/contextual/ Mongo.rb', line 617

def last!
  last || Raise_document_not_Found_error
end

# lengthInteger也称为:大小

返回数据库中与查询选择器匹配的文档数。

例子:

获取长度。

context.length

返回:

  • ( Integer )

    文档数量。



287
288
289
# File 'lib/mongoid/contextual/ Mongo.rb', line 287

def 长度
  self.数数
end

# limit (value) ⇒ mongo

限制从数据库返回的文档数量。

例子:

限制文档。

context.limit(20)

参数:

  • ( Integer )

    要返回的文档数量。

返回:

  • ( mongo )

    上下文。



300
301
302
# File 'lib/mongoid/contextual/ Mongo.rb', line 300

def limit()
  @view = 查看.limit()  self
end

# load_async对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

安排任务以加载上下文的文档。

根据 Mongoid 配置,计划任务可以在调用者的线程上立即执行,也可以计划异步执行。



776
777
778
# File 'lib/mongoid/contextual/ Mongo.rb', line 776

def load_async
  @documents_loader ||= DocumentsLoader.new(查看, klass, 条件)
end

# map_reduce (map, reduce) ⇒ MapReduce

从上下文启动 map/reduce 操作。

例子:

启动 map/reduce。

context.map_reduce(map, reduce)

参数:

  • map ( string )

    地图JavaScript函数。

  • 化简(reduce) ( string )

    reduce JavaScript函数。

返回:

  • ( MapReduce )

    map/reduce 惰性包装器。



313
314
315
# File 'lib/mongoid/contextual/ Mongo.rb', line 313

def map_reduce(map, 化简(reduce))
  MapReduce.new(集合, 条件, map, 化简(reduce))
end

# pick (*fields) ⇒对象 | Array<Object>

从数据库中选取单个字段值。

例子:

选择一个字段。

context.pick(:_id)

参数:

  • *fields ( [ string | Symbol ]... )

    要选择的字段。

返回:

  • ( Object | Array<Object> )

    选取的值。



361
362
363
# File 'lib/mongoid/contextual/ Mongo.rb', line 361

def pick(*字段)
  limit(1).采摘(*字段).first
end

# pluck (*fields) ⇒ Array<Object> | Array<Array<Object>>

从数据库中提取字段值。 为上下文在数据库中找到的每个文档返回一个结果。 结果根据其 Mongoid字段类型进行规范化。 请注意,结果可能包含重复项和 nil 值。

例子:

提取一个字段。

context.pluck(:_id)

参数:

  • *fields ( [ string | Symbol ]... )

    要提取的字段,其中可能包括使用点表示法的嵌套字段。

返回:

  • ( Array<Object> | Array<Array<Object>> )

    提取的值。 如果 *fields 参数包含单个值,则大量中的每个结果都将是单个值。 否则,大量中的每个结果都将是值大量。



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/mongoid/contextual/ Mongo.rb', line 333

def 采摘(*字段)
  # 多个字段可以映射到同一字段名称。 例如,采摘
  #a字段及其 _translations字段映射到数据库中的同一字段。
  # 因此,我们需要追踪请求的字段。
  normalized_field_names = []
  normalized_select = 字段.注入({}) do |哈希, f|
    db_fn = klass.database_field_name(f)
    normalized_field_names.推动(db_fn)
    哈希[klass.clean_localized_field_names(f)] = true
    哈希
  end

  查看.投影(normalized_select).化简(reduce)([]) do |弹拨, doc|
    values = normalized_field_names.map do |n|
      提取值(doc, n)
    end
    弹拨 << (values.size == 1 ? values.first : values)
  end
end

#文档| nil

获取数据库中条件选择器的第二个文档。

例子:

获取第二个文档。

context.second

返回:

  • (Document | nil)

    第二个文档;如果未找到文档,则为 nil。



627
628
629
# File 'lib/mongoid/contextual/ Mongo.rb', line 627

def 
  retrieve_nth(1)
end

#秒!文档

获取数据库中条件选择器的第二个文档,如果未找到,则引发错误。

例子:

获取第二个文档。

context.second!

返回:

  • (文档)

    第二个文档。

引发:



641
642
643
# File 'lib/mongoid/contextual/ Mongo.rb', line 641

def 第二!
   || Raise_document_not_Found_error
end

# Second_to_last文档| nil

获取数据库中条件选择器的倒数第二个文档。

找到。

例子:

获取倒数第二个文档。

context.second_to_last

返回:

  • (Document | nil)

    倒数第二个文档;如果没有,则为 nil



725
726
727
# File 'lib/mongoid/contextual/ Mongo.rb', line 725

def Second_to_last
  retrieve_nth_to_last(1)
end

# Second_to_last!文档

获取数据库中条件选择器的倒数第二个文档,如果未找到任何文档,则引发错误。

例子:

获取倒数第二个文档。

context.second_to_last!

返回:

  • (文档)

    倒数第二个文档。

引发:



739
740
741
# File 'lib/mongoid/contextual/ Mongo.rb', line 739

def Second_to_last!
  Second_to_last || Raise_document_not_Found_error
end

# skip (value) ⇒ mongo

跳过所提供数量的文档。

例子:

跳过文档。

context.skip(20)

参数:

  • ( Integer )

    要跳过的文档数量。

返回:

  • ( mongo )

    上下文。



480
481
482
# File 'lib/mongoid/contextual/ Mongo.rb', line 480

def 跳过()
  @view = 查看.跳过()  self
end

#sort(values = nil, &block) ⇒ Mongo

按提供的规范对文档进行排序。

例子:

对文档进行排序。

context.sort(name: -1, title: 1)

参数:

  • values 哈希 (默认为: nil

    以字段/方向 ( 1 /- 1 ) 对形式对值进行排序。

返回:

  • ( mongo )

    上下文。



493
494
495
496
497
498
499
500
501
502
# File 'lib/mongoid/contextual/ Mongo.rb', line 493

def sort(values = nil, )
  if block_given?
    ()
  else
    #更新条件
    @criteria = 条件.order_by(values)
    apply_option(:sort)
    self
  end
end

#take(limit = nil) ⇒ Document | Array<Document>

从数据库中获取给定数量的文档。

例子:

获取10文档

context.take(10)

参数:

  • limit ( Integer | nil ) (默认为: nil

    要返回的文档数或为零。

返回:

  • ( Document | Array< Document > )

    文档列表,如果未给出值,则为一个文档。



374
375
376
377
378
379
380
381
382
# File 'lib/mongoid/contextual/ Mongo.rb', line 374

def 采取(limit = nil)
  if limit
    limit(limit).to_a
  else
    # 首先执行 to_a,这样 Mongo#first 方法就不会被使用,
    # 结果未排序。
    limit(1).to_a.first
  end
end

#take!Document

从数据库中获取一份文档,如果没有,则引发错误。

例子:

获取文档

context.take!

返回:

引发:



393
394
395
396
397
398
399
400
401
# File 'lib/mongoid/contextual/ Mongo.rb', line 393

def 采取!
  # 首先执行 to_a,这样 Mongo#first 方法就不会被使用,
  # 结果未排序。
  if fst = limit(1).to_a.first
    fst
  else
    提高 Errors::DocumentNotFound.new(klass, nil, nil)
  end
end

#tally (字段)→哈希

获取单个字段值的计数哈希。 例如,如果数据库中有以下文档:

{ _id: 1, age: 21 }
{ _id: 2, age: 21 }
{ _id: 3, age: 22 }

Model.tally("age")

会产生以下结果:

{ 21 => 2, 22 => 1 }

统计大量或 embeds_many 关联内的字段时:

{ _id: 1, array: [ { x: 1 }, { x: 2 } ] }
{ _id: 2, array: [ { x: 1 }, { x: 2 } ] }
{ _id: 3, array: [ { x: 1 }, { x: 3 } ] }

Model.tally("array.x")

生成的哈希值的键是数组:

{ [ 1, 2 ] => 2, [ 1, 3 ] => 1 }

请注意,如果统计哈希数组中的某个元素,并且某些哈希中不存在该键,则计数不会在生成的哈希中包含这些 nil 键:

{ _id: 1, array: [ { x: 1 }, { x: 2 }, { y: 3 } ] }

Model.tally("array.x")
# => { [ 1, 2 ] => 1 }

参数:

  • 字段 ( string | Symbol )

    字段名称。

返回:

  • (哈希)

    计数的哈希值。



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/mongoid/contextual/ Mongo.rb', line 440

def 计数(字段)
  名称 = klass.clean_localized_field_names(字段)

  fld = klass.traverse_association_tree(名称)
  管道 = [ { " $ 群组 " => { _id: " $ #{ name } ", 计数: { " $sum ": 1 } } } ]管道 unshift ( " $match " =>查看.过滤 除非查看过滤空白?

  集合聚合管道  reduce ( { } ) do |计数, doc | is_translation = " #{ name } _translations " == 字段 . to_s
    val = doc [ " _id " ] key = if val . is_a? ( Array ) val . map do | v | v demogoize_with_field ( fld , v , is_translation ) end else demogoize_with_field ( fld , val , is_translation ) end # 键已经存在于计数哈希中的唯一时间是# 值以不同方式存储在数据库中,但# demogoize 为相同的值。 #发生这种情况的一个很好的示例是使用本地化字段时。虽然服务器查询不会群组具有不同语言的其他值的哈希值分组在一起,但# demogoized 值只是当前区域设置的翻译, # 它可以在多个不等哈希值中保持相同。    计数[] ||= 0计数[] += doc [ " counts " ]计数end end

# Third文档| nil

在数据库中获取条件选择器的第三个文档。

例子:

获取第三个文档。

context.third

返回:

  • (Document | nil)

    第三个文档;如果未找到,则为 nil。



651
652
653
# File 'lib/mongoid/contextual/ Mongo.rb', line 651

def 第三
  retrieve_nth(2)
end

# Third!文档

在数据库中获取条件选择器的第三个文档,如果未找到,则引发错误。

例子:

获取第三个文档。

context.third!

返回:

  • (文档)

    第三个文档。

引发:



665
666
667
# File 'lib/mongoid/contextual/ Mongo.rb', line 665

def 第三!
  第三 || Raise_document_not_Found_error
end

# Third_to_last文档| nil

获取数据库中倒数第三个文档作为条件的选择器。

找到。

例子:

获取倒数第三个文档。

context.third_to_last

返回:

  • (Document | nil)

    倒数第三个文档;如果没有,则为 nil



751
752
753
# File 'lib/mongoid/contextual/ Mongo.rb', line 751

def Third_to_last
  retrieve_nth_to_last(2)
end

# Third_to_last!文档

获取数据库中倒数第三个文档作为条件选择器,如果未找到,则引发错误。

例子:

获取倒数第三个文档。

context.third_to_last!

返回:

  • (文档)

    倒数第三个文档。

引发:



765
766
767
# File 'lib/mongoid/contextual/ Mongo.rb', line 765

def Third_to_last!
  Third_to_last || Raise_document_not_Found_error
end

# update (attributes = nil, opts = {}) ⇒ nil | false

自动更新第一个匹配的文档。

例子:

更新第一个匹配的文档。

context.update({ "$set" => { name: "Smiths" }})

参数:

  • 属性 哈希 (默认为: nil

    文档的新属性。

  • opts 哈希 (默认为: {}

    更新操作选项。

选项哈希 ( opts ):

  • :array_filters 数组

    一设立筛选器,用于指定应将更新应用哪些大量元素。

返回:

  • ( nil | false )

    如果未提供属性,则为 False。



516
517
518
# File 'lib/mongoid/contextual/ Mongo.rb', line 516

def update(属性 = nil, opts = {})
  update_documents(属性, :update_one, opts)
end

# update_all (attributes = nil, opts = {}) ⇒ nil | false

自动更新所有匹配的文档。

例子:

更新所有匹配的文档。

context.update_all({ "$set" => { name: "Smiths" }})

参数:

  • 属性 哈希 (默认为: nil

    每个文档的新属性。

  • opts 哈希 (默认为: {}

    更新操作选项。

选项哈希 ( opts ):

  • :array_filters 数组

    一设立筛选器,用于指定应将更新应用哪些大量元素。

返回:

  • ( nil | false )

    如果未提供属性,则为 False。



532
533
534
# File 'lib/mongoid/contextual/ Mongo.rb', line 532

def update_all(属性 = nil, opts = {})
  update_documents(属性, :update_many, opts)
end