Module: Mongo::Collection::View::Writable
- Included in:
- Mongo::Collection::View
- Defined in:
- lib/mongo/collection/view/writable.rb
Overview
Defines write related behavior 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.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/mongo/collection/view/writable.rb', line 166 def delete_many(opts = {}) delete_doc = { Operation::Q => filter, Operation::LIMIT => 0 } with_session(opts) do |session| write_concern = write_concern_with_session(session) nro_write_with_retry(session, write_concern) 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 => write_concern, :session => session ).execute(server, client: client) end end end |
#delete_one(opts = {}) ⇒ Result
Remove a document from the collection.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/mongo/collection/view/writable.rb', line 196 def delete_one(opts = {}) delete_doc = { Operation::Q => filter, Operation::LIMIT => 1 } with_session(opts) do |session| write_concern = write_concern_with_session(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, client: client) 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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mongo/collection/view/writable.rb', line 50 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 with_session(opts) do |session| applied_write_concern = applied_write_concern(session) cmd[:writeConcern] = applied_write_concern. if applied_write_concern write_with_retry(session, applied_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, client: client) end end.first['value'] end |
#find_one_and_replace(replacement, opts = {}) ⇒ BSON::Document
Finds a single document and replaces it.
93 94 95 |
# File 'lib/mongo/collection/view/writable.rb', line 93 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.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/mongo/collection/view/writable.rb', line 124 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 if opts[:bypass_document_validation] cmd[:bypassDocumentValidation] = true end value = with_session(opts) do |session| applied_write_concern = applied_write_concern(opts[:session]) cmd[:writeConcern] = applied_write_concern. if applied_write_concern write_with_retry(session, applied_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, client: client) 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.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/mongo/collection/view/writable.rb', line 232 def replace_one(replacement, opts = {}) update_doc = { Operation::Q => filter, Operation::U => replacement, } if opts[:upsert] update_doc['upsert'] = true end with_session(opts) do |session| write_concern = write_concern_with_session(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, client: client) end end end |
#update_many(spec, opts = {}) ⇒ Result
Update documents in the collection.
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/mongo/collection/view/writable.rb', line 278 def update_many(spec, opts = {}) update_doc = { Operation::Q => filter, Operation::U => spec, Operation::MULTI => true, } if opts[:upsert] update_doc['upsert'] = true end with_session(opts) do |session| write_concern = write_concern_with_session(session) nro_write_with_retry(session, write_concern) 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 => write_concern, :bypass_document_validation => !!opts[:bypass_document_validation], :session => session ).execute(server, client: client) end end end |
#update_one(spec, opts = {}) ⇒ Result
Update a single document in the collection.
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/mongo/collection/view/writable.rb', line 323 def update_one(spec, opts = {}) update_doc = { Operation::Q => filter, Operation::U => spec, } if opts[:upsert] update_doc['upsert'] = true end with_session(opts) do |session| write_concern = write_concern_with_session(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, client: client) end end end |