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.

  • :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.

Returns:

  • (Result)

    The response from the database.

Since:

  • 2.1.0



1023
1024
1025
# File 'lib/mongo/collection.rb', line 1023

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