模块:Mongoid::Association::Embedded::Batchable
Overview
包含对嵌入式文档批处理执行操作的行为。
实例方法摘要折叠
-
# batch_clear ( Docs ) ⇒ 数组
一次滑动即可清除关联中的所有Docs 。
-
#batch_insert(docs) ⇒ Array<Hash>
以批量推送方式插入新文档(带有 $each 的 $push)。
-
# batch_remove ( Docs , method =: 删除) ⇒ 对象
批量删除以 $pullAll 或 $pull 形式提供的文档。
-
# batch_replace ( Docs ) ⇒ Array<Hash>
将提供的文档批量替换为 $设立。
位置包含的方法
实例方法详细信息
# batch_clear ( Docs ) ⇒数组
一次滑动即可清除关联中的所有Docs 。
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mongoid/association/embedded/batchable.rb', line 35 def batch_clear(docs) pre_process_batch_remove(docs, :删除) 除非 docs.空? 集合.find(选择器).update_one( 按位置(选择器, " $unset " => { 路径 => true }), 会话: _session ) # 这解决了用户设置、清除和重置 #嵌入式文档. 以前,由于嵌入式文档是 # 已标记为非“new_record”,因此不会持久保存到 # 第二次。 此更改修复了该问题并允许其持久化。 docs.每 { |doc| doc.new_record = true } post_process_batch_remove(docs, :删除) end _unscoped.清除 end |
#batch_insert(docs) ⇒ Array<Hash>
以批处理推送方式插入新文档(带有 $each 的 $push)。 这可确保所有回调都在适当的时间运行,并且仅向数据库发出1请求。
23 24 25 |
# File 'lib/mongoid/association/embedded/batchable.rb', line 23 def batch_insert(docs) execute_batch_push(docs) end |
# batch_remove ( Docs , method =: 删除) ⇒对象
批量删除以 $pullAll 或 $pull 形式提供的文档。
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mongoid/association/embedded/batchable.rb', line 59 def batch_remove(docs, 方法 = :删除) # 如果_id为 nil,则无法通过搜索来使用 $pull 和删除 # ID 。 因此,我们必须将 pullAll 与文档的 # 属性。 删除 = pre_process_batch_remove(docs, 方法) pull, pull_alls = 删除.分区 { |o| !o[" _id "].nil? } if !_base.持续存在? post_process_batch_remove(docs, 方法) 除非 docs.空? return reIndex end if !docs.空? if !pull.空? 集合.find(选择器).update_one( 按位置(选择器, " $pull " => { 路径 => { " _id " => { " $in " => pull.采摘(" _id ") } } }), 会话: _session ) end if !pull_alls.空? 集合.find(选择器).update_one( 按位置(选择器, " $pullAll " => { 路径 => pull_alls }), 会话: _session ) end post_process_batch_remove(docs, 方法) else 集合.find(选择器).update_one( 按位置(选择器, " $set " => { 路径 => [] }), 会话: _session ) end reIndex end |
# batch_replace ( Docs ) ⇒ Array<Hash>
将提供的文档批量替换为 $设立。
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/mongoid/association/embedded/batchable.rb', line 102 def batch_replace(docs) if docs.空白? if _Assigning? && !空? _base.Delayed_atomic_sets.删除(路径) clear_atomic_path_cache _base.add_atomic_unset(first) target_duplicate = _target.dup pre_process_batch_remove(target_duplicate, :删除) post_process_batch_remove(target_duplicate, :删除) else batch_remove(_target.dup) end elsif _target != docs _base.Delayed_atomic_sets.删除(路径) 除非 _Assigning? docs = normalize_docs(docs).compact _target.清除 和 _unscoped.清除 _base.Delayed_atomic_unsets.删除(路径) clear_atomic_path_cache inserts = execute_batch_set(docs) add_atomic_sets(inserts) end end |