Module: Mongoid::Persistable::Upsertable

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

Overview

Defines behaviour for persistence operations that upsert documents.

Since:

  • 4.0.0

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.

Since:

  • 3.0.0


22
23
24
25
26
# File 'lib/mongoid/persistable/upsertable.rb', line 22

def upsert(options = {})
  prepare_upsert(options) do
    collection.find(atomic_selector).update_one(as_document, upsert: true)
  end
end