Module: Mongo::Collection::View::Writable

Included in:
Mongo::Collection::View
Defined in:
lib/mongo/collection/view/writable.rb

Overview

Defines write related behaviour for collection view.

Since:

  • 2.0.0

Instance Method Summary collapse

Instance Method Details

#delete_many(opts = {}) ⇒ Result

Remove documents from the collection.

Examples:

Remove multiple documents from the collection.

collection_view.delete_many

Parameters:

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

    The options.

Options Hash (opts):

  • :collation (Hash)

    The collation to use.

Returns:

  • (Result)

    The response from the database.

Since:

  • 2.0.0



135
136
137
# File 'lib/mongo/collection/view/writable.rb', line 135

def delete_many(opts = {})
  remove(0, opts)
end

#delete_one(opts = {}) ⇒ Result

Remove a document from the collection.

Examples:

Remove a single document from the collection.

collection_view.delete_one

Parameters:

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

    The options.

Options Hash (opts):

  • :collation (Hash)

    The collation to use.

Returns:

  • (Result)

    The response from the database.

Since:

  • 2.0.0



151
152
153
# File 'lib/mongo/collection/view/writable.rb', line 151

def delete_one(opts = {})
  remove(1, opts)
end

#find_one_and_delete(opts = {}) ⇒ BSON::Document?

Finds a single document in the database via findAndModify and deletes it, returning the original document.

Examples:

Find one document and delete it.

view.find_one_and_delete

Parameters:

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

    The options.

Options Hash (opts):

  • :collation (Hash)

    The collation to use.

Returns:

  • (BSON::Document, nil)

    The document, if found.

Since:

  • 2.0.0



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mongo/collection/view/writable.rb', line 37

def find_one_and_delete(opts = {})
  cmd = { :findandmodify => collection.name, :query => filter, :remove => true }
  cmd[:fields] = projection if projection
  cmd[:sort] = sort if sort
  cmd[:maxTimeMS] = max_time_ms if max_time_ms
  cmd[:writeConcern] = write_concern.options if write_concern

  write_with_retry do
    server = next_primary
    apply_collation!(cmd, server, opts)

    Operation::Commands::Command.new({
                                      :selector => cmd,
                                      :db_name => database.name
                                     }).execute(server).first['value']
  end
end

#find_one_and_replace(replacement, opts = {}) ⇒ BSON::Document

Finds a single document and replaces it.

Examples:

Find a document and replace it, returning the original.

view.find_one_and_replace({ name: 'test' }, :return_document => :before)

Find a document and replace it, returning the new document.

view.find_one_and_replace({ name: 'test' }, :return_document => :after)

Parameters:

  • replacement (BSON::Document)

    The replacement.

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

    The options.

  • options (Hash)

    a customizable set of options

Options Hash (opts):

  • :return_document (Symbol)

    Either :before or :after.

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

Returns:

  • (BSON::Document)

    The document.

Since:

  • 2.0.0



77
78
79
# File 'lib/mongo/collection/view/writable.rb', line 77

def find_one_and_replace(replacement, opts = {})
  find_one_and_update(replacement, opts)
end

#find_one_and_update(document, opts = {}) ⇒ BSON::Document

Finds a single document and updates it.

Examples:

Find a document and update it, returning the original.

view.find_one_and_update({ "$set" => { name: 'test' }}, :return_document => :before)

Parameters:

  • document (BSON::Document)

    The updates.

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

    The options.

Options Hash (opts):

  • :return_document (Symbol)

    Either :before or :after.

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

  • :write_concern (Hash)

    The write concern options. Defaults to the collection's write concern.

  • :collation (Hash)

    The collation to use.

Returns:

  • (BSON::Document)

    The document.

Since:

  • 2.0.0



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mongo/collection/view/writable.rb', line 100

def find_one_and_update(document, opts = {})
  cmd = { :findandmodify => collection.name, :query => filter }
  cmd[:update] = document
  cmd[:fields] = projection if projection
  cmd[:sort] = sort if sort
  cmd[:new] = !!(opts[:return_document] && opts[:return_document] == :after)
  cmd[:upsert] = opts[:upsert] if opts[:upsert]
  cmd[:maxTimeMS] = max_time_ms if max_time_ms
  cmd[:bypassDocumentValidation] = !!opts[:bypass_document_validation]
  cmd[:writeConcern] = write_concern.options if write_concern

  value = write_with_retry do
    server = next_primary
    apply_collation!(cmd, server, opts)

    Operation::Commands::Command.new({
                                      :selector => cmd,
                                      :db_name => database.name
                                     }).execute(server).first['value']
  end
  value unless value.nil? || value.empty?
end

#replace_one(replacement, opts = {}) ⇒ Result

Replaces a single document in the database with the new document.

Examples:

Replace a single document.

collection_view.replace_one({ name: 'test' })

Parameters:

  • replacement (Hash)

    The replacement document.

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

    The options.

Options Hash (opts):

  • :upsert (true, false)

    Whether to upsert if the document doesn't exist.

  • :collation (Hash)

    The collation to use.

Returns:

  • (Result)

    The response from the database.

Since:

  • 2.0.0



170
171
172
# File 'lib/mongo/collection/view/writable.rb', line 170

def replace_one(replacement, opts = {})
  update(replacement, false, opts)
end

#update_many(spec, opts = {}) ⇒ Result

Update documents in the collection.

Examples:

Update multiple documents in the collection.

collection_view.update_many('$set' => { name: 'test' })

Parameters:

  • spec (Hash)

    The update statement.

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

    The options.

Options Hash (opts):

  • :upsert (true, false)

    Whether to upsert if the document doesn't exist.

  • :collation (Hash)

    The collation to use.

Returns:

  • (Result)

    The response from the database.

Since:

  • 2.0.0



189
190
191
# File 'lib/mongo/collection/view/writable.rb', line 189

def update_many(spec, opts = {})
  update(spec, true, opts)
end

#update_one(spec, opts = {}) ⇒ Result

Update a single document in the collection.

Examples:

Update a single document in the collection.

collection_view.update_one('$set' => { name: 'test' })

Parameters:

  • spec (Hash)

    The update statement.

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

    The options.

Options Hash (opts):

  • :upsert (true, false)

    Whether to upsert if the document doesn't exist.

  • :collation (Hash)

    The collation to use.

Returns:

  • (Result)

    The response from the database.

Since:

  • 2.0.0



208
209
210
# File 'lib/mongo/collection/view/writable.rb', line 208

def update_one(spec, opts = {})
  update(spec, false, opts)
end