Module: Mongoid::Association::Embedded::Batchable
- Includes:
- Positional
- Included in:
- EmbedsMany::Proxy
- Defined in:
- lib/mongoid/association/embedded/batchable.rb
Overview
Contains behaviour for executing operations in batch on embedded documents.
Instance Method Summary collapse
-
#batch_clear(docs) ⇒ Array
Clear all of the docs out of the relation in a single swipe.
-
#batch_insert(docs) ⇒ Array<Hash>
Insert new documents as a batch push ($push with $each).
-
#batch_remove(docs, method = :delete) ⇒ Object
Batch remove the provided documents as a $pullAll.
-
#batch_replace(docs) ⇒ Array<Hash>
Batch replace the provided documents as a $set.
Methods included from Positional
Instance Method Details
#batch_clear(docs) ⇒ Array
Clear all of the docs out of the relation in a single swipe.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mongoid/association/embedded/batchable.rb', line 37 def batch_clear(docs) pre_process_batch_remove(docs, :delete) unless docs.empty? collection.find(selector).update_one( positionally(selector, "$unset" => { path => true }), session: _session ) post_process_batch_remove(docs, :delete) end _unscoped.clear end |
#batch_insert(docs) ⇒ Array<Hash>
Insert new documents as a batch push ($push with $each). This ensures that all callbacks are run at the appropriate time and only 1 request is made to the database.
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 = :delete) ⇒ Object
Batch remove the provided documents as a $pullAll.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mongoid/association/embedded/batchable.rb', line 58 def batch_remove(docs, method = :delete) removals = pre_process_batch_remove(docs, method) if !docs.empty? collection.find(selector).update_one( positionally(selector, "$pullAll" => { path => removals }), session: _session ) post_process_batch_remove(docs, method) end reindex end |
#batch_replace(docs) ⇒ Array<Hash>
Batch replace the provided documents as a $set.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mongoid/association/embedded/batchable.rb', line 80 def batch_replace(docs) if docs.blank? if _assigning? && !empty? _base.add_atomic_unset(first) target_duplicate = _target.dup pre_process_batch_remove(target_duplicate, :delete) post_process_batch_remove(target_duplicate, :delete) else batch_remove(_target.dup) end elsif _target != docs _base.delayed_atomic_sets.clear unless _assigning? docs = normalize_docs(docs).compact _target.clear and _unscoped.clear inserts = execute_batch_set(docs) add_atomic_sets(inserts) end end |