类:Mongoid::Contextual::Mongo

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

Overview

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

在命名空间下定义

类: DocumentsLoader

常量摘要折叠

OPTIONS =

选项常量。

%i[提示
limit
跳过
sort
batch_size
max_scan
max_time_ms
snapshot
comment

cursor_type
排序规则].冻结

Atomic中包含的常量

Atomic::UPDATES

实例属性摘要折叠

Queryable中包含的属性

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

实例方法摘要折叠

Queryable中包含的方法

#blank?

Association::EagerLoadable 中包含的方法

#create_pipeline#eager_load#eager_load_with_lookup#eager_loadable?#preload#preload_for_lookup#switch_local_and_foreign_fields?

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)

参数:



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

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

实例属性详细信息

# documents_loader对象(只读)

返回属性 documents_loader 的值。



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

def documents_loader
  @documents_loader
end

#视图哈希(只读)

对条件运行解释。

例子:

解释标准。

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

参数:

  • 选项 (哈希)

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

返回:

  • (哈希)

    解释结果。



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

def 查看
  @view
end

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



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

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 )

    匹配项的数量。



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

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.



112
113
114
# File 'lib/mongoid/contextual/ Mongo.rb', line 112

def 删除
  查看.delete_many.Deleted_Count
end

#destroynil也称为: destroy_all

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

例子:

销毁所有文档。

context.destroy

返回:

  • ( nil )

    Nil.



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

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

# distinct (field) ⇒ Array<Object>

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

例子:

获取不同的值。

context.distinct(:name)

参数:

  • 字段 ( string | Symbol )

    字段的名称。

返回:

  • ( Array<Object> )

    字段的非重复值。



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

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

返回:

  • (枚举器)

    枚举器。



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

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 那样在 Enumerable 上定义(使用区块)。

例子:

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

context.estimated_count

参数:

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

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

返回:

  • ( Integer )

    匹配项的数量。



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

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

    提高 Mongoid::Errors::InvalidEstimatedCountCriteria.new(klass)

  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。



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

def 存在吗?(id_or_conditions = : none)
  return false if 查看.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

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

例子:

获取第五个文档。

context.fifth

返回:

  • (Document | nil)

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



684
685
686
# File 'lib/mongoid/contextual/ Mongo.rb', line 684

def 第五
  retrieve_nth(4)
end

#第五!文档

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

例子:

获取第五个文档。

context.fifth!

返回:

  • (文档)

    第五个文档。

引发:



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

def 第五!
  第五 || Raise_document_not_Found_error
end

# find_first对象

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

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



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

def find_first
  return 除非 raw_doc = 查看.first

  doc = 工厂.from_db(klass, raw_doc, 条件)
  ager_load([ doc ]).first
end

# find_one_and_delete文档

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

例子:

执行命令。

context.find_one_and_delete

返回:

  • (文档)

    命令的结果。



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

def find_one_and_delete
  return 除非 doc = 查看.find_one_and_delete

  工厂.from_db(klass, doc)
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 )

    如果document不存在,则创建该document。

返回:

  • (文档)

    命令的结果。



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

def find_one_and_replace(替换, 选项 = {})
  return 除非 doc = 查看.find_one_and_replace(替换, 选项)

  工厂.from_db(klass, doc)
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 )

    如果document不存在,则创建该document。

返回:

  • (文档)

    命令的结果。



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

def find_one_and_update(update, 选项 = {})
  return 除非 doc = 查看.find_one_and_update(update, 选项)

  工厂.from_db(klass, doc)
end

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

注意:

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

Get数据库中符合条件的选择器的第一个document。

例子:

获取第一个文档。

context.first

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (Document | nil)

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



535
536
537
538
539
540
541
# File 'lib/mongoid/contextual/ Mongo.rb', line 535

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!不保证顺序。

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

例子:

获取第一个文档。

context.first!

返回:

  • (文档)

    第一个文档。

引发:



560
561
562
# File 'lib/mongoid/contextual/ Mongo.rb', line 560

def 首先!
  first || Raise_document_not_Found_error
end

#fourth文档| nil

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

例子:

获取第四个文档。

context.fourth

返回:

  • (Document | nil)

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



660
661
662
# File 'lib/mongoid/contextual/ Mongo.rb', line 660

def 第四
  retrieve_nth(3)
end

#fourth!文档

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

例子:

获取第四个文档。

context.fourth!

返回:

  • (文档)

    第四个文档。

引发:



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

def 第四!
  第四 || Raise_document_not_Found_error
end

#last(limit = nil) ⇒ Document | nil

注意:

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

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

例子:

获取最后一个文档。

context.last

参数:

  • limit 整数 (默认为: nil

    要返回的文档数量。

返回:

  • (Document | nil)

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



578
579
580
581
582
583
584
# File 'lib/mongoid/contextual/ Mongo.rb', line 578

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!不保证顺序。

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

例子:

获取最后一个文档。

context.last!

返回:

  • (文档)

    最后一个文档。

引发:



602
603
604
# File 'lib/mongoid/contextual/ Mongo.rb', line 602

def last!
  last || Raise_document_not_Found_error
end

# lengthInteger也称为:大小

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

例子:

获取长度。

context.length

返回:

  • ( Integer )

    文档数量。



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

def 长度
  数数
end

# limit (value) ⇒ mongo

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

例子:

限制文档。

context.limit(20)

参数:

  • ( Integer )

    要返回的文档数量。

返回:

  • ( mongo )

    上下文。



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

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

# load_async对象

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

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

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



761
762
763
# File 'lib/mongoid/contextual/ Mongo.rb', line 761

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 惰性包装器。



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

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

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

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

例子:

选择一个字段。

context.pick(:_id)

参数:

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

    要选择的字段。

返回:

  • ( Object | Array<Object> )

    选取的值。



349
350
351
# File 'lib/mongoid/contextual/ Mongo.rb', line 349

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 参数包含单个值,则大量中的每个结果都将是单个值。 否则,大量中的每个结果都将是值大量。



332
333
334
335
336
337
338
339
# File 'lib/mongoid/contextual/ Mongo.rb', line 332

def 采摘(*字段)
  # 多个字段可以映射到同一字段名称。示例,
  # 将一个字段及其 _translations字段映射到相同的
  #数据库中的字段。因此,我们需要准备
  # 专门用于投影。
  prep = prepare_puck(字段, prepare_projection: true)
  pluck_from_documents(查看.投影(prep[:projection]), prep[:field_names])
end

#文档| nil

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

例子:

获取第二个文档。

context.second

返回:

  • (Document | nil)

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



612
613
614
# File 'lib/mongoid/contextual/ Mongo.rb', line 612

def 
  retrieve_nth(1)
end

#秒!文档

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

例子:

获取第二个文档。

context.second!

返回:

  • (文档)

    第二个文档。

引发:



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

def 第二!
   || Raise_document_not_Found_error
end

# Second_to_last文档| nil

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

找到。

例子:

获取倒数第二个文档。

context.second_to_last

返回:

  • (Document | nil)

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



710
711
712
# File 'lib/mongoid/contextual/ Mongo.rb', line 710

def Second_to_last
  retrieve_nth_to_last(1)
end

# Second_to_last!文档

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

例子:

获取倒数第二个文档。

context.second_to_last!

返回:

  • (文档)

    倒数第二个文档。

引发:



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

def Second_to_last!
  Second_to_last || Raise_document_not_Found_error
end

# skip (value) ⇒ mongo

跳过所提供数量的文档。

例子:

跳过文档。

context.skip(20)

参数:

  • ( Integer )

    要跳过的文档数量。

返回:

  • ( mongo )

    上下文。



465
466
467
# File 'lib/mongoid/contextual/ Mongo.rb', line 465

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

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

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

例子:

对文档进行排序。

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

参数:

  • values 哈希 (默认为: nil

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

返回:

  • ( mongo )

    上下文。



478
479
480
481
482
483
484
485
486
487
# File 'lib/mongoid/contextual/ Mongo.rb', line 478

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

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



362
363
364
365
366
367
368
369
370
# File 'lib/mongoid/contextual/ Mongo.rb', line 362

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!

返回:

引发:



381
382
383
384
385
386
387
# File 'lib/mongoid/contextual/ Mongo.rb', line 381

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

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

    字段名称。

返回:

  • (哈希)

    计数的哈希值。



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'lib/mongoid/contextual/ Mongo.rb', line 426

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

  fld = klass.traverse_association_tree(名称)
  管道 = [ { '$ 群组' => { _id: " $ #{ name } ", 计数: { '$sum': 1 } } } ]
管道unshift('$match'=> 查看过滤器除非视图过滤器空白?集合聚合管道每个_with_object({}) do |doc 计数|
    is_translation = "#{name}_translations" == 字段.to_sval = 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 值只是当前区域设置设置的翻译, # 它可以在多个不等哈希值中保持相同。talies[key] ||= 0
    talies[key] += doc['counts']
  end
end

# Third文档| nil

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

例子:

获取第三个文档。

context.third

返回:

  • (Document | nil)

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



636
637
638
# File 'lib/mongoid/contextual/ Mongo.rb', line 636

def 第三
  retrieve_nth(2)
end

# Third!文档

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

例子:

获取第三个文档。

context.third!

返回:

  • (文档)

    第三个文档。

引发:



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

def 第三!
  第三 || Raise_document_not_Found_error
end

# Third_to_last文档| nil

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

找到。

例子:

获取倒数第三个文档。

context.third_to_last

返回:

  • (Document | nil)

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



736
737
738
# File 'lib/mongoid/contextual/ Mongo.rb', line 736

def Third_to_last
  retrieve_nth_to_last(2)
end

# Third_to_last!文档

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

例子:

获取倒数第三个文档。

context.third_to_last!

返回:

  • (文档)

    倒数第三个文档。

引发:



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

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。



501
502
503
# File 'lib/mongoid/contextual/ Mongo.rb', line 501

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。



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

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