Module: Mongoid::Persistable::Upsertable

Included in:
Mongoid::Persistable
Defined in:
lib/mongoid/persistable/upsertable.rb

Overview

Defines behavior for persistence operations that upsert documents.

Instance Method Summary collapse

Instance Method Details

#upsert(options = {}) ⇒ true

Perform an upsert of the document. If the document does not exist in the database, then Mongo will insert a new one, otherwise the fields will get overwritten with new values on the existing document.

Examples:

Upsert the document.

document.upsert

Parameters:

  • options (Hash) (defaults to: {})

    The validation options.

Returns:

  • (true)

    True.



19
20
21
22
23
24
# File 'lib/mongoid/persistable/upsertable.rb', line 19

def upsert(options = {})
  prepare_upsert(options) do
    collection.find(atomic_selector).replace_one(
        as_attributes, upsert: true, session: _session)
  end
end