module Mongo::Collection::View::Writable
Defines write related behaviour for collection view.
@since 2.0.0
Public Instance Methods
Remove documents from the collection.
@example Remove multiple documents from the collection.
collection_view.delete_many
@param [ Hash ] opts The options.
@option opts [ Hash ] :collation The collation to use.
@return [ Result ] The response from the database.
@since 2.0.0
# File lib/mongo/collection/view/writable.rb, line 131 def delete_many(opts = {}) remove(0, opts) end
Remove a document from the collection.
@example Remove a single document from the collection.
collection_view.delete_one
@param [ Hash ] opts The options.
@option opts [ Hash ] :collation The collation to use.
@return [ Result ] The response from the database.
@since 2.0.0
# File lib/mongo/collection/view/writable.rb, line 147 def delete_one(opts = {}) remove(1, opts) end
Finds a single document in the database via findAndModify and deletes it, returning the original document.
@example Find one document and delete it.
view.find_one_and_delete
@return [ BSON::Document, nil ] The document, if found.
@since 2.0.0
# 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 cmd[:writeConcern] = write_concern.options if write_concern server = next_primary validate_collation!(server, options) cmd[:collation] = options[:collation] if options[:collation] write_with_retry do Operation::Commands::Command.new({ :selector => cmd, :db_name => database.name }).execute(server).first['value'] end end
Finds a single document and replaces it.
@example Find a document and replace it, returning the original.
view.find_one_and_replace({ name: 'test' }, :return_document => :before)
@example Find a document and replace it, returning the new document.
view.find_one_and_replace({ name: 'test' }, :return_document => :after)
@param [ BSON::Document ] replacement The replacement. @param [ Hash ] opts The options.
@option opts [ Symbol ] :return_document Either :before or :after. @option opts [ true, false ] :upsert Whether to upsert if the document doesn't exist. @option opts [ true, false ] :bypass_document_validation Whether or
not to skip document level validation.
@option options [ Hash ] :write_concern The write concern options.
Defaults to the collection's write concern.
@return [ BSON::Document ] The document.
@since 2.0.0
# File lib/mongo/collection/view/writable.rb, line 73 def find_one_and_replace(replacement, opts = {}) find_one_and_update(replacement, opts) end
Finds a single document and updates it.
@example Find a document and update it, returning the original.
view.find_one_and_update({ "$set" => { name: 'test' }}, :return_document => :before)
@param [ BSON::Document ] document The updates. @param [ Hash ] opts The options.
@option opts [ Symbol ] :return_document Either :before or :after. @option opts [ true, false ] :upsert Whether to upsert if the document doesn't exist. @option opts [ true, false ] :bypass_document_validation Whether or
not to skip document level validation.
@option opts [ Hash ] :write_concern The write concern options.
Defaults to the collection's write concern.
@return [ BSON::Document ] The document.
@since 2.0.0
# File lib/mongo/collection/view/writable.rb, line 95 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.options if write_concern server = next_primary validate_collation!(server, options) cmd[:collation] = options[:collation] if options[:collation] value = write_with_retry do Operation::Commands::Command.new({ :selector => cmd, :db_name => database.name }).execute(server).first['value'] end value unless value.nil? || value.empty? end
Replaces a single document in the database with the new document.
@example Replace a single document.
collection_view.replace_one({ name: 'test' })
@param [ Hash ] replacement The replacement document. @param [ Hash ] opts The options.
@option opts [ true, false ] :upsert Whether to upsert if the
document doesn't exist.
@option opts [ Hash ] :collation The collation to use.
@return [ Result ] The response from the database.
@since 2.0.0
# File lib/mongo/collection/view/writable.rb, line 166 def replace_one(replacement, opts = {}) update(replacement, false, opts) end
Update documents in the collection.
@example Update multiple documents in the collection.
collection_view.update_many('$set' => { name: 'test' })
@param [ Hash ] spec The update statement. @param [ Hash ] opts The options.
@option opts [ true, false ] :upsert Whether to upsert if the
document doesn't exist.
@option opts [ Hash ] :collation The collation to use.
@return [ Result ] The response from the database.
@since 2.0.0
# File lib/mongo/collection/view/writable.rb, line 185 def update_many(spec, opts = {}) update(spec, true, opts) end
Update a single document in the collection.
@example Update a single document in the collection.
collection_view.update_one('$set' => { name: 'test' })
@param [ Hash ] spec The update statement. @param [ Hash ] opts The options.
@option opts [ true, false ] :upsert Whether to upsert if the
document doesn't exist.
@option opts [ Hash ] :collation The collation to use.
@return [ Result ] The response from the database.
@since 2.0.0
# File lib/mongo/collection/view/writable.rb, line 204 def update_one(spec, opts = {}) update(spec, false, opts) end
Private Instance Methods
# File lib/mongo/collection/view/writable.rb, line 210 def remove(value, opts) server = next_primary validate_collation!(server, opts) delete_doc = { Operation::Q => filter, Operation::LIMIT => value } # We must extract the collation at the String key as well so that if w == 0, # an error can be raised later when an OpCode is used. # Otherwise, the collation will silently not be sent. if collation = opts[:collation] || opts[Operation::COLLATION] delete_doc[:collation] = collation end write_with_retry do Operation::Write::Delete.new( :delete => delete_doc, :db_name => collection.database.name, :coll_name => collection.name, :write_concern => collection.write_concern ).execute(server) end end
# File lib/mongo/collection/view/writable.rb, line 230 def update(spec, multi, opts) server = next_primary validate_collation!(server, opts) update_doc = { Operation::Q => filter, Operation::U => spec, Operation::MULTI => multi, Operation::UPSERT => !!opts[:upsert] } # We must extract the collation at the String key as well so that if w == 0, # an error can be raised later when an OpCode is used. # Otherwise, the collation will silently not be sent. if collation = opts[:collation] || opts[Operation::COLLATION] update_doc[:collation] = collation end write_with_retry do Operation::Write::Update.new( :update => update_doc, :db_name => collection.database.name, :coll_name => collection.name, :write_concern => collection.write_concern, :bypass_document_validation => !!opts[:bypass_document_validation] ).execute(server) end end