类:Mongoid::Contextual::Mongo
- 继承:
-
对象
- 对象
- Mongoid::Contextual::Mongo
- 扩展方式:
- 可转发
- 定义于:
- lib/mongoid/contextual/ mongo .rb ,
lib/mongoid/contextual/ mongo /documents_loader.rb
Overview
上下文对象,用于对保存在数据库中但尚未加载到应用程序内存中的文档执行批量查询和持久化操作。
在命名空间下定义
常量摘要折叠
- OPTIONS =
选项常量。
%i[提示 limit 跳过 sort batch_size max_scan max_time_ms snapshot comment 读 cursor_type 排序规则].冻结
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
获取数据库中条件选择器的第五个 document。
-
#第五! ⇒ 文档
获取数据库中条件选择器的第五个document,如果未找到,则引发错误。
-
# 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)
Get数据库中符合条件的选择器的第一个document。
-
# first! ⇒ 文档
获取数据库中条件选择器的第一个 document,如果未找到任何 document,则引发错误。
-
#fourth ⇒ 文档 | nil
获取数据库中条件选择器的第四个 document。
-
#fourth! ⇒ 文档
获取数据库中条件选择器的第四个document,如果未找到,则引发错误。
-
#initialize (criteria) ⇒ mongo
构造函数
创建新的mongo上下文。
-
#last(limit = nil) ⇒ Document | nil
获取数据库中符合条件的选择器的最后一个文档。
-
#last! ⇒ Document
获取数据库中条件选择器的最后一个 document,如果未找到任何 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
获取数据库中条件选择器的第二个document。
-
#秒! ⇒ 文档
获取数据库中条件选择器的第二个document,如果未找到,则引发错误。
-
# Second_to_last ⇒ 文档 | nil
获取数据库中条件选择器的倒数第二个 document。
-
# Second_to_last! ⇒ 文档
获取数据库中条件选择器的倒数第二个 document,如果未找到任何 document,则引发错误。
-
# skip (value) ⇒ mongo
跳过所提供数量的文档。
-
#sort(values = nil, &block) ⇒ Mongo
按提供的规范对文档进行排序。
-
#take (limit = nil) ⇒ 文档 |数组<Document>
从数据库中获取给定数量的文档。
-
#take! ⇒ Document
从数据库中获取一份文档,如果没有,则引发错误。
-
#tally (字段)→ 哈希
获取单个字段值的计数哈希值。
-
# Third ⇒ 文档 | nil
在数据库中获取条件选择器的第三个文档。
-
# Third! ⇒ 文档
在数据库中获取条件选择器的第三个document,如果未找到,则引发错误。
-
# Third_to_last ⇒ 文档 | nil
获取数据库中倒数第三个document作为条件的选择器。
-
# Third_to_last! ⇒ 文档
获取数据库中倒数第三个document作为条件选择器,如果未找到,则引发错误。
-
# update (attributes = nil, opts = {}) ⇒ nil | false
自动更新第一个匹配的文档。
-
#update_all(attributes = nil, opts = {}) ⇒ nil | false
自动更新所有匹配的文档。
Queryable中包含的方法
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上下文。 这会将操作委托给底层驱动程序。
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) end |
实例属性详细信息
# documents_loader ⇒对象(只读)
返回属性 documents_loader 的值。
53 54 55 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 53 def documents_loader @documents_loader end |
#视图⇒哈希(只读)
对条件运行解释。
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
获取与查询匹配的文档数。
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 |
# delete ⇒ nil也称为: delete_all
删除数据库中与选择器匹配的所有文档。
112 113 114 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 112 def 删除 查看.delete_many.Deleted_Count end |
#destroy ⇒ nil也称为: destroy_all
销毁数据库中与选择器匹配的所有文档。
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>
获取数据库中所提供字段的非重复值。
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文档,否则返回一个枚举。
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 上定义(使用区块)。
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 索引。
该上下文是否存在任何文档。
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。
684 685 686 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 684 def 第五 retrieve_nth(4) end |
#第五! ⇒文档
获取数据库中条件选择器的第五个document,如果未找到,则引发错误。
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。
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。
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。
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。
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,则引发错误。
560 561 562 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 560 def 首先! first || Raise_document_not_Found_error end |
#fourth ⇒文档| nil
获取数据库中条件选择器的第四个 document。
660 661 662 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 660 def 第四 retrieve_nth(3) end |
#fourth! ⇒文档
获取数据库中条件选择器的第四个document,如果未找到,则引发错误。
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 不保证顺序。
获取数据库中符合条件的选择器的最后一个文档。
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,则引发错误。
602 603 604 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 602 def last! last || Raise_document_not_Found_error end |
# length ⇒ Integer也称为:大小
返回数据库中与查询选择器匹配的文档数。
286 287 288 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 286 def 长度 数数 end |
# limit (value) ⇒ 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 操作。
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>
从数据库中选取单个字段值。
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 值。
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。
612 613 614 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 612 def 秒 retrieve_nth(1) end |
#秒! ⇒文档
获取数据库中条件选择器的第二个document,如果未找到,则引发错误。
626 627 628 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 626 def 第二! 秒 || Raise_document_not_Found_error end |
# Second_to_last ⇒文档| nil
获取数据库中条件选择器的倒数第二个 document。
找到。
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,则引发错误。
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
跳过所提供数量的文档。
465 466 467 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 465 def 跳过(值) @view = 查看.跳过(值) 和 self end |
#sort(values = nil, &block) ⇒ 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>
从数据库中获取给定数量的文档。
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
从数据库中获取一份文档,如果没有,则引发错误。
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 }
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
在数据库中获取条件选择器的第三个文档。
636 637 638 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 636 def 第三 retrieve_nth(2) end |
# Third! ⇒文档
在数据库中获取条件选择器的第三个document,如果未找到,则引发错误。
650 651 652 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 650 def 第三! 第三 || Raise_document_not_Found_error end |
# Third_to_last ⇒文档| nil
获取数据库中倒数第三个document作为条件的选择器。
找到。
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作为条件选择器,如果未找到,则引发错误。
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
自动更新第一个匹配的文档。
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
自动更新所有匹配的文档。
517 518 519 |
# File 'lib/mongoid/contextual/ Mongo.rb', line 517 def update_all(属性 = nil, opts = {}) update_documents(属性, :update_many, opts) end |