Module: Mongoid::Persistable::Upsertable

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

Overview

Defines behavior 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



24
25
26
27
28
29
# File 'build/mongoid-master/lib/mongoid/persistable/upsertable.rb', line 24

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