Module: Mongo::Collection::View::Writable
- Included in:
- Mongo::Collection::View
- Defined in:
- lib/mongo/collection/view/writable.rb
Overview
Defines write related behaviour for collection view.
Instance Method Summary collapse
-
#delete_many ⇒ Result
Remove documents from the collection.
-
#delete_one ⇒ Result
Remove a document from the collection.
-
#find_one_and_delete ⇒ BSON::Document?
Finds a single document in the database via findAndModify and deletes it, returning the original document.
-
#find_one_and_replace(replacement, opts = {}) ⇒ BSON::Document
Finds a single document and replaces it.
-
#find_one_and_update(document, opts = {}) ⇒ BSON::Document
Finds a single document and updates it.
-
#replace_one(replacement, opts = {}) ⇒ Result
Replaces a single document in the database with the new document.
-
#update_many(spec, opts = {}) ⇒ Result
Update documents in the collection.
-
#update_one(spec, opts = {}) ⇒ Result
Update a single document in the collection.
Instance Method Details
#delete_many ⇒ Result
Remove documents from the collection.
115 116 117 |
# File 'lib/mongo/collection/view/writable.rb', line 115 def delete_many remove(0) end |
#delete_one ⇒ Result
Remove a document from the collection.
127 128 129 |
# File 'lib/mongo/collection/view/writable.rb', line 127 def delete_one remove(1) end |
#find_one_and_delete ⇒ BSON::Document?
Finds a single document in the database via findAndModify and deletes it, returning the original document.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mongo/collection/view/writable.rb', line 33 def find_one_and_delete cmd = { :findandmodify => collection.name, :query => filter, :remove => true } cmd[:fields] = projection if projection cmd[:sort] = sort if sort cmd[:maxTimeMS] = max_time_ms if max_time_ms wc = [:write_concern] || (collection.write_concern && collection.write_concern.) cmd[:writeConcern] = wc if wc write_with_retry do database.command(cmd).first['value'] end end |
#find_one_and_replace(replacement, opts = {}) ⇒ BSON::Document
Finds a single document and replaces it.
67 68 69 |
# File 'lib/mongo/collection/view/writable.rb', line 67 def find_one_and_replace(replacement, opts = {}) find_one_and_update(replacement, opts) end |
#find_one_and_update(document, opts = {}) ⇒ BSON::Document
Finds a single document and updates it.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mongo/collection/view/writable.rb', line 89 def find_one_and_update(document, opts = {}) cmd = { :findandmodify => collection.name, :query => filter } cmd[:update] = document cmd[:fields] = projection if projection cmd[:sort] = sort if sort cmd[:new] = !!(opts[:return_document] && opts[:return_document] == :after) cmd[:upsert] = opts[:upsert] if opts[:upsert] cmd[:maxTimeMS] = max_time_ms if max_time_ms cmd[:bypassDocumentValidation] = !!opts[:bypass_document_validation] wc = [:write_concern] || (collection.write_concern && collection.write_concern.) cmd[:writeConcern] = wc if wc write_with_retry do value = database.command(cmd).first['value'] value unless value.nil? || value.empty? end end |
#replace_one(replacement, opts = {}) ⇒ Result
Replaces a single document in the database with the new document.
145 146 147 |
# File 'lib/mongo/collection/view/writable.rb', line 145 def replace_one(replacement, opts = {}) update(replacement, false, opts) end |
#update_many(spec, opts = {}) ⇒ Result
Update documents in the collection.
163 164 165 |
# File 'lib/mongo/collection/view/writable.rb', line 163 def update_many(spec, opts = {}) update(spec, true, opts) end |
#update_one(spec, opts = {}) ⇒ Result
Update a single document in the collection.
181 182 183 |
# File 'lib/mongo/collection/view/writable.rb', line 181 def update_one(spec, opts = {}) update(spec, false, opts) end |