类:Mongoid::Contextual::Mongo
- 继承:
-
对象
- 对象
- Mongoid::Contextual::Mongo
- 扩展方式:
- 可转发
- 包括:
- Enumerable、 Association::EagerLoadable 、 Atomic 、 Aggregable::Mongo 、 Queryable
- 定义于:
- lib/mongoid/contextual/ mongo .rb ,
lib/mongoid/contextual/ mongo /documents_loader.rb
Overview
上下文对象,用于对保存在数据库中但尚未加载到应用程序内存中的文档执行批量查询和持久化操作。
在命名空间下定义
常量摘要折叠
- OPTIONS =
选项常量。
[ :hint, :limit, :skip, :sort, :batch_size, :max_scan, :max_time_ms, :快照, :comment, :read, :cursor_type, :collation ].冻结
Atomic中包含的常量
实例属性摘要折叠
-
# documents_loader ⇒ 对象
只读
返回属性 documents_loader 的值。
-
#视图⇒ 哈希
只读
对条件运行解释。
- # view mongo集合视图。 (TheMongocollectionview。) ⇒ 对象 只读
Queryable中包含的属性
#collection 、 #collection 要查询的集合。 、 #criteria 、 #criteria 上下文的条件。 、 #klass 、 #klass 条件的 klass。
实例方法摘要折叠
-
# count (options = {}, &block) ⇒ Integer
获取与查询匹配的文档数。
-
# 删除 ⇒ nil (又名:#delete_all)
删除数据库中与选择器匹配的所有文档。
-
#destroy ⇒ nil (也:#destroy_all)
销毁数据库中与选择器匹配的所有文档。
-
# distinct (field) ⇒ Array<Object>
获取数据库中所提供字段的非重复值。
-
#each (&block) ⇒ 枚举器
遍历上下文。
-
#estimated_count (options = {}) ⇒ Integer
获取与查询匹配的文档的估计数量。
-
#是否存在? (id_or_conditions = :none) ⇒ true | false
该上下文是否存在任何文档。
-
#第五个⇒ 文档 | nil
获取数据库中条件选择器的第五个文档。
-
#第五! ⇒ 文档
获取数据库中条件选择器的第五个文档,如果未找到,则引发错误。
-
# find_first ⇒ 对象
private
返回第一个结果而不应用排序。
-
# find_one_and_delete ⇒ 文档
执行查找和修改命令,用于 MongoDB 的 $findAndModify。
-
# find_one_and_replace (replacement, options = {}) ⇒ 文档
执行查找和修改命令,用于 MongoDB 的 $findAndModify。
-
# find_one_and_update (update, options = {}) ⇒ 文档
执行查找和修改命令,用于 MongoDB 的 $findAndModify。
-
# first (limit = nil) ⇒ 文档 | nil (又作:#one)
获取数据库中符合条件的选择器的第一个文档。
-
# first! ⇒ 文档
获取数据库中条件选择器的第一个文档,如果未找到任何文档,则引发错误。
-
#fourth ⇒ 文档 | nil
获取数据库中条件选择器的第四个文档。
-
#fourth! ⇒ 文档
获取数据库中条件选择器的第四个文档,如果未找到,则引发错误。
-
#initialize (criteria) ⇒ mongo
构造函数
创建新的mongo上下文。
-
#last(limit = nil) ⇒ Document | nil
获取数据库中符合条件的选择器的最后一个文档。
-
#last! ⇒ Document
获取数据库中条件选择器的最后一个文档,如果未找到任何文档,则引发错误。
-
# length ⇒ Integer (也称为:#size)
返回数据库中与查询选择器匹配的文档数。
-
# limit (value) ⇒ mongo
限制从数据库返回的文档数量。
-
# load_async ⇒ 对象
private
安排任务以加载上下文的文档。
-
# map_reduce (map, reduce) ⇒ MapReduce
从上下文启动 map/reduce 操作。
-
# pick (*fields) ⇒ 对象 | Array<Object>
从数据库中选取单个字段值。
-
# pluck (*fields) ⇒ Array<Object> | Array<Array<Object>>
从数据库中提取字段值。
-
#秒⇒ 文档 | nil
获取数据库中条件选择器的第二个文档。
-
#秒! ⇒ 文档
获取数据库中条件选择器的第二个文档,如果未找到,则引发错误。
-
# Second_to_last ⇒ 文档 | nil
获取数据库中条件选择器的倒数第二个文档。
-
# Second_to_last! ⇒ 文档
获取数据库中条件选择器的倒数第二个文档,如果未找到任何文档,则引发错误。
-
# skip (value) ⇒ mongo
跳过所提供数量的文档。
-
#sort(values = nil, &block) ⇒ Mongo
按提供的规范对文档进行排序。
-
#take (limit = nil) ⇒ 文档 |数组<Document>
从数据库中获取给定数量的文档。
-
#take! ⇒ Document
从数据库中获取一份文档,如果没有,则引发错误。
-
#tally (字段)→ 哈希
获取单个字段值的计数哈希值。
-
# Third ⇒ 文档 | nil
在数据库中获取条件选择器的第三个文档。
-
# Third! ⇒ 文档
在数据库中获取条件选择器的第三个文档,如果未找到,则引发错误。
-
# Third_to_last ⇒ 文档 | nil
获取数据库中倒数第三个文档作为条件的选择器。
-
# Third_to_last! ⇒ 文档
获取数据库中倒数第三个文档作为条件选择器,如果未找到,则引发错误。
-
# update (attributes = nil, opts = {}) ⇒ nil | false
自动更新第一个匹配的文档。
-
#update_all(attributes = nil, opts = {}) ⇒ nil | false
自动更新所有匹配的文档。
Queryable中包含的方法
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上下文。 这会将操作委托给底层驱动程序。
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) end |
实例属性详细信息
# documents_loader ⇒对象(只读)
返回属性 documents_loader 的值。
54 55 56 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 54 def documents_loader @documents_loader end |
#视图⇒哈希(只读)
对条件运行解释。
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
获取与查询匹配的文档数。
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 |
# delete ⇒ nil也称为: delete_all
删除数据库中与选择器匹配的所有文档。
114 115 116 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 114 def 删除 查看.delete_many.Deleted_Count end |
#destroy ⇒ nil也称为: destroy_all
销毁数据库中与选择器匹配的所有文档。
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>
获取数据库中所提供字段的非重复值。
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文档,否则返回一个枚举。
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 上定义(使用区块)。
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 索引。
该上下文是否存在任何文档。
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
获取数据库中条件选择器的第五个文档。
699 700 701 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 699 def 第五 retrieve_nth(4) end |
#第五! ⇒文档
获取数据库中条件选择器的第五个文档,如果未找到,则引发错误。
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。 这将删除找到的文档。
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。
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。
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 不保证顺序。
获取数据库中符合条件的选择器的第一个文档。
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! 不保证顺序。
获取数据库中条件选择器的第一个文档,如果未找到任何文档,则引发错误。
575 576 577 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 575 def 首先! first || Raise_document_not_Found_error end |
#fourth ⇒文档| nil
获取数据库中条件选择器的第四个文档。
675 676 677 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 675 def 第四 retrieve_nth(3) end |
#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 不保证顺序。
获取数据库中符合条件的选择器的最后一个文档。
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! 不保证顺序。
获取数据库中条件选择器的最后一个文档,如果未找到任何文档,则引发错误。
617 618 619 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 617 def last! last || Raise_document_not_Found_error end |
# length ⇒ Integer也称为:大小
返回数据库中与查询选择器匹配的文档数。
287 288 289 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 287 def 长度 self.数数 end |
# limit (value) ⇒ 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 操作。
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>
从数据库中选取单个字段值。
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 值。
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
获取数据库中条件选择器的第二个文档。
627 628 629 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 627 def 秒 retrieve_nth(1) end |
#秒! ⇒文档
获取数据库中条件选择器的第二个文档,如果未找到,则引发错误。
641 642 643 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 641 def 第二! 秒 || Raise_document_not_Found_error end |
# Second_to_last ⇒文档| 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! ⇒文档
获取数据库中条件选择器的倒数第二个文档,如果未找到任何文档,则引发错误。
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
跳过所提供数量的文档。
480 481 482 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 480 def 跳过(值) @view = 查看.跳过(值) 和 self end |
#sort(values = nil, &block) ⇒ 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>
从数据库中获取给定数量的文档。
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
从数据库中获取一份文档,如果没有,则引发错误。
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 }
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
在数据库中获取条件选择器的第三个文档。
651 652 653 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 651 def 第三 retrieve_nth(2) end |
# Third! ⇒文档
在数据库中获取条件选择器的第三个文档,如果未找到,则引发错误。
665 666 667 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 665 def 第三! 第三 || Raise_document_not_Found_error end |
# Third_to_last ⇒文档| 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! ⇒文档
获取数据库中倒数第三个文档作为条件选择器,如果未找到,则引发错误。
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
自动更新第一个匹配的文档。
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
自动更新所有匹配的文档。
532 533 534 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 532 def update_all(属性 = nil, opts = {}) update_documents(属性, :update_many, opts) end |