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.
Constant Summary collapse
- ARRAY_FILTERS =
The array filters field constant.
'array_filters'.freeze
Instance Method Summary collapse
-
#delete_many(opts = {}) ⇒ Result
Remove documents from the collection.
-
#delete_one(opts = {}) ⇒ Result
Remove a document from the collection.
-
#find_one_and_delete(opts = {}) ⇒ 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(opts = {}) ⇒ Result
Remove documents from the collection.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/mongo/collection/view/writable.rb', line 147 def delete_many(opts = {}) delete_doc = { Operation::Q => filter, Operation::LIMIT => 0 } with_session(opts) do |session| legacy_write_with_retry do |server| apply_collation!(delete_doc, server, opts) Operation::Delete.new( :deletes => [ delete_doc ], :db_name => collection.database.name, :coll_name => collection.name, :write_concern => collection.write_concern, :session => session ).execute(server) end end end |
#delete_one(opts = {}) ⇒ Result
Remove a document from the collection.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/mongo/collection/view/writable.rb', line 175 def delete_one(opts = {}) delete_doc = { Operation::Q => filter, Operation::LIMIT => 1 } write_concern = collection.write_concern with_session(opts) do |session| write_with_retry(session, write_concern) do |server, txn_num| apply_collation!(delete_doc, server, opts) Operation::Delete.new( :deletes => [ delete_doc ], :db_name => collection.database.name, :coll_name => collection.name, :write_concern => write_concern, :session => session, :txn_num => txn_num ).execute(server) end end end |
#find_one_and_delete(opts = {}) ⇒ BSON::Document?
Finds a single document in the database via findAndModify and deletes it, returning the original document.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mongo/collection/view/writable.rb', line 42 def find_one_and_delete(opts = {}) 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 cmd[:writeConcern] = write_concern. if write_concern with_session(opts) do |session| write_with_retry(session, write_concern) do |server, txn_num| apply_collation!(cmd, server, opts) Operation::Command.new( :selector => cmd, :db_name => database.name, :session => session, :txn_num => txn_num ).execute(server) end end.first['value'] end |
#find_one_and_replace(replacement, opts = {}) ⇒ BSON::Document
Finds a single document and replaces it.
84 85 86 |
# File 'lib/mongo/collection/view/writable.rb', line 84 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.
an update should apply.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/mongo/collection/view/writable.rb', line 109 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] cmd[:writeConcern] = write_concern. if write_concern value = with_session(opts) do |session| write_with_retry(session, write_concern) do |server, txn_num| apply_collation!(cmd, server, opts) apply_array_filters!(cmd, server, opts) Operation::Command.new( :selector => cmd, :db_name => database.name, :session => session, :txn_num => txn_num ).execute(server) end end.first['value'] value unless value.nil? || value.empty? end |
#replace_one(replacement, opts = {}) ⇒ Result
Replaces a single document in the database with the new document.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/mongo/collection/view/writable.rb', line 208 def replace_one(replacement, opts = {}) update_doc = { Operation::Q => filter, Operation::U => replacement, Operation::MULTI => false, Operation::UPSERT => !!opts[:upsert] } write_concern = collection.write_concern with_session(opts) do |session| write_with_retry(session, write_concern) do |server, txn_num| apply_collation!(update_doc, server, opts) apply_array_filters!(update_doc, server, opts) Operation::Update.new( :updates => [ update_doc ], :db_name => collection.database.name, :coll_name => collection.name, :write_concern => write_concern, :bypass_document_validation => !!opts[:bypass_document_validation], :session => session, :txn_num => txn_num ).execute(server) end end end |
#update_many(spec, opts = {}) ⇒ Result
Update documents in the collection.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/mongo/collection/view/writable.rb', line 250 def update_many(spec, opts = {}) update_doc = { Operation::Q => filter, Operation::U => spec, Operation::MULTI => true, Operation::UPSERT => !!opts[:upsert] } with_session(opts) do |session| legacy_write_with_retry do |server| apply_collation!(update_doc, server, opts) apply_array_filters!(update_doc, server, opts) Operation::Update.new( :updates => [ update_doc ], :db_name => collection.database.name, :coll_name => collection.name, :write_concern => collection.write_concern, :bypass_document_validation => !!opts[:bypass_document_validation], :session => session ).execute(server) end end end |
#update_one(spec, opts = {}) ⇒ Result
Update a single document in the collection.
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/mongo/collection/view/writable.rb', line 288 def update_one(spec, opts = {}) update_doc = { Operation::Q => filter, Operation::U => spec, Operation::MULTI => false, Operation::UPSERT => !!opts[:upsert] } write_concern = collection.write_concern with_session(opts) do |session| write_with_retry(session, write_concern) do |server, txn_num| apply_collation!(update_doc, server, opts) apply_array_filters!(update_doc, server, opts) Operation::Update.new( :updates => [ update_doc ], :db_name => collection.database.name, :coll_name => collection.name, :write_concern => write_concern, :bypass_document_validation => !!opts[:bypass_document_validation], :session => session, :txn_num => txn_num ).execute(server) end end end |