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'
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 | nil
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.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/mongo/collection/view/writable.rb', line 247 def delete_many(opts = {}) with_session(opts) do |session| write_concern = if opts[:write_concern] WriteConcern.get(opts[:write_concern]) else write_concern_with_session(session) end QueryCache.clear_namespace(collection.namespace) delete_doc = { Operation::Q => filter, Operation::LIMIT => 0, hint: opts[:hint], collation: opts[:collation] || opts['collation'] || collation, }.compact context = Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts(opts) ) operation = Operation::Delete.new( deletes: [ delete_doc ], db_name: collection.database.name, coll_name: collection.name, write_concern: write_concern, bypass_document_validation: !!opts[:bypass_document_validation], session: session, let: opts[:let], comment: opts[:comment] ) tracer.trace_operation(operation, context, op_name: 'deleteMany') do nro_write_with_retry(write_concern, context: context) do |connection, _txn_num, context| operation.execute_with_connection(connection, context: context) end end end end |
#delete_one(opts = {}) ⇒ Result
Remove a document from the collection.
311 312 313 314 315 316 317 318 319 320 321 322 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 311 def delete_one(opts = {}) with_session(opts) do |session| write_concern = if opts[:write_concern] WriteConcern.get(opts[:write_concern]) else write_concern_with_session(session) end QueryCache.clear_namespace(collection.namespace) delete_doc = { Operation::Q => filter, Operation::LIMIT => 1, hint: opts[:hint], collation: opts[:collation] || opts['collation'] || collation, }.compact context = Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts(opts) ) write_with_retry(write_concern, context: context) do |connection, txn_num, context| Operation::Delete.new( deletes: [ delete_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, let: opts[:let], comment: opts[:comment] ).execute_with_connection(connection, context: context) 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.
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 93 94 95 96 97 98 99 100 |
# File 'lib/mongo/collection/view/writable.rb', line 61 def find_one_and_delete(opts = {}) with_session(opts) do |session| write_concern = if opts[:write_concern] WriteConcern.get(opts[:write_concern]) else write_concern_with_session(session) end QueryCache.clear_namespace(collection.namespace) cmd = { findAndModify: collection.name, query: filter, remove: true, fields: projection, sort: sort, maxTimeMS: max_time_ms, bypassDocumentValidation: opts[:bypass_document_validation], hint: opts[:hint], collation: opts[:collation] || opts['collation'] || collation, let: opts[:let], comment: opts[:comment], }.compact context = Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts(opts) ) write_with_retry(write_concern, context: context) do |connection, txn_num, context| Operation::WriteCommand.new( selector: cmd, db_name: database.name, write_concern: write_concern, session: session, txn_num: txn_num ).execute_with_connection(connection, context: context) end end.first&.fetch('value', nil) end |
#find_one_and_replace(replacement, opts = {}) ⇒ BSON::Document
Finds a single document and replaces it.
132 133 134 |
# File 'lib/mongo/collection/view/writable.rb', line 132 def find_one_and_replace(replacement, opts = {}) find_one_and_update(replacement, opts) end |
#find_one_and_update(document, opts = {}) ⇒ BSON::Document | nil
Finds a single document and updates it.
an update should apply.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/mongo/collection/view/writable.rb', line 174 def find_one_and_update(document, opts = {}) value = with_session(opts) do |session| write_concern = if opts[:write_concern] WriteConcern.get(opts[:write_concern]) else write_concern_with_session(session) end QueryCache.clear_namespace(collection.namespace) cmd = { findAndModify: collection.name, query: filter, arrayFilters: opts[:array_filters] || opts['array_filters'], update: document, fields: projection, sort: sort, new: !!(opts[:return_document] && opts[:return_document] == :after), upsert: opts[:upsert], maxTimeMS: max_time_ms, bypassDocumentValidation: opts[:bypass_document_validation], hint: opts[:hint], collation: opts[:collation] || opts['collation'] || collation, let: opts[:let], comment: opts[:comment] }.compact context = Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts(opts) ) operation = Operation::WriteCommand.new( selector: cmd, db_name: database.name, write_concern: write_concern, session: session ) value = tracer.trace_operation(operation, context, op_name: 'findOneAndUpdate') do write_with_retry(write_concern, context: context) do |connection, txn_num, context| operation.txn_num = txn_num operation.execute_with_connection(connection, context: context) end end.first&.fetch('value', nil) value unless value.nil? || value.empty? end end |
#replace_one(replacement, opts = {}) ⇒ Result
Replaces a single document in the database with the new document.
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/mongo/collection/view/writable.rb', line 384 def replace_one(replacement, opts = {}) with_session(opts) do |session| write_concern = if opts[:write_concern] WriteConcern.get(opts[:write_concern]) else write_concern_with_session(session) end validate_replacement_documents!(replacement) QueryCache.clear_namespace(collection.namespace) update_doc = { Operation::Q => filter, arrayFilters: opts[:array_filters] || opts['array_filters'], Operation::U => replacement, hint: opts[:hint], collation: opts[:collation] || opts['collation'] || collation, sort: opts[:sort] || opts['sort'], }.compact update_doc['upsert'] = true if opts[:upsert] context = Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts(opts) ) write_with_retry(write_concern, context: context) do |connection, txn_num, context| 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, let: opts[:let], comment: opts[:comment] ).execute_with_connection(connection, context: context) end end end |
#update_many(spec, opts = {}) ⇒ Result
Update documents in the collection.
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/mongo/collection/view/writable.rb', line 458 def update_many(spec, opts = {}) with_session(opts) do |session| write_concern = if opts[:write_concern] WriteConcern.get(opts[:write_concern]) else write_concern_with_session(session) end validate_update_documents!(spec) QueryCache.clear_namespace(collection.namespace) update_doc = { Operation::Q => filter, arrayFilters: opts[:array_filters] || opts['array_filters'], Operation::U => spec, Operation::MULTI => true, hint: opts[:hint], collation: opts[:collation] || opts['collation'] || collation, }.compact update_doc['upsert'] = true if opts[:upsert] context = Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts(opts) ) nro_write_with_retry(write_concern, context: context) do |connection, _txn_num, context| 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, let: opts[:let], comment: opts[:comment] ).execute_with_connection(connection, context: context) end end end |
#update_one(spec, opts = {}) ⇒ Result
Update a single document in the collection.
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 |
# File 'lib/mongo/collection/view/writable.rb', line 536 def update_one(spec, opts = {}) with_session(opts) do |session| write_concern = if opts[:write_concern] WriteConcern.get(opts[:write_concern]) else write_concern_with_session(session) end validate_update_documents!(spec) QueryCache.clear_namespace(collection.namespace) update_doc = { Operation::Q => filter, arrayFilters: opts[:array_filters] || opts['array_filters'], Operation::U => spec, hint: opts[:hint], collation: opts[:collation] || opts['collation'] || collation, sort: opts[:sort] || opts['sort'], }.compact update_doc['upsert'] = true if opts[:upsert] context = Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts(opts) ) operation = 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, let: opts[:let], comment: opts[:comment] ) tracer.trace_operation(operation, context) do write_with_retry(write_concern, context: context) do |connection, txn_num, context| operation.txn_num = txn_num operation.execute_with_connection(connection, context: context) end end end end |