Method: Mongo::Collection#update_one

Defined in:
lib/mongo/collection.rb

#update_one(filter, update, options = {}) ⇒ Result

Update a single document in the collection.

Examples:

Update a single document in the collection.

collection.update_one({ name: 'test'}, '$set' => { name: 'test1'})

Parameters:

  • filter (Hash)

    The filter to use.

  • update (Hash | Array<Hash>)

    The update document or pipeline.

  • options (Hash) (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.

  • :array_filters (Array)

    A set of filters specifying to which array elements an update should apply.

  • :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 updates if the query matches multiple documents. The first document matched by the sort order will be updated. This option is only supported by servers >= 8.0. Older servers will report an error for using this option.

Returns:

  • (Result)

    The response from the database.

Since:

  • 2.1.0



1132
1133
1134
# File 'lib/mongo/collection.rb', line 1132

def update_one(filter, update, options = {})
  find(filter, options).update_one(update, options)
end