Method: Mongo::Collection#replace_one

Defined in:
lib/mongo/collection.rb

#replace_one(filter, replacement, options = {}) ⇒ Result

Replaces a single document in the collection with the new document.

Examples:

Replace a single document.

collection.replace_one({ name: 'test' }, { name: 'test1' })

Parameters:

  • The filter to use.

  • The replacement document..

  • (defaults to: {})

    The options.

Options Hash (options):

  • :upsert (true | false)

    Whether to upsert if the document doesn’t exist.

  • :bypass_document_validation (true | false)

    Whether or not to skip document level validation.

  • :collation (Hash)

    The collation to use.

  • :session (Session)

    The session to use.

  • :timeout_ms (Integer)

    The operation timeout in milliseconds. Must be a non-negative integer. An explicit value of 0 means infinite. The default value is unset which means the value is inherited from the collection or the database or the client.

  • :hint (Hash | String)

    The index to use for this operation. May be specified as a Hash (e.g. { _id: 1 }) or a String (e.g. “id”).

  • :let (Hash)

    Mapping of variables to use in the command. See the server documentation for details.

  • :sort (Hash)

    Specifies which document the operation replaces if the query matches multiple documents. The first document matched by the sort order will be replaced. This option is only supported by servers >= 8.0. Older servers will report an error for using this option.

Returns:

  • The response from the database.

Since:

  • 2.1.0



1061
1062
1063
# File 'lib/mongo/collection.rb', line 1061

def replace_one(filter, replacement, options = {})
  find(filter, options).replace_one(replacement, options)
end