Class: Mongo::Protocol::Update

Inherits:
Message
  • Object
show all
Defined in:
lib/mongo/protocol/update.rb

Overview

MongoDB Wire protocol Update message.

This is a client request message that is sent to the server in order to update documents matching the provided query.

The default is to update a single document. In order to update many at a time users should set the :multi_update flag for the update.

If an upsert (update or insert) is desired, users can set the :upsert flag in order to indicate they would like to insert the merged selector and update if no document matching the update query currently exists.

Defined Under Namespace

Classes: Upconverter

Constant Summary

Constants inherited from Message

Message::BATCH_SIZE, Message::COLLECTION, Message::LIMIT, Message::MAX_MESSAGE_SIZE, Message::ORDERED, Message::Q

Instance Attribute Summary

Attributes inherited from Message

#request_id

Instance Method Summary collapse

Methods inherited from Message

#==, deserialize, #hash, #replyable?, #serialize, #set_request_id

Constructor Details

#initialize(database, collection, selector, update, options = {}) ⇒ Update

Creates a new Update message

Examples:

Update single document

Update.new('xgen', 'users', {:name => 'Tyler'}, {:name => 'Bob'})

Perform a multi update

Update.new('xgen', 'users',
  {:age => 20}, {:age => 21}, :flags => [:multi_update])

Perform an upsert

Update.new('xgen', 'users', {:name => 'Tyler'}, :flags => [:upsert])

Parameters:

  • database (String, Symbol)

    The database to update.

  • collection (String, Symbol)

    The collection to update.

  • selector (Hash)

    The update selector.

  • update (Hash)

    The update to perform.

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

    The additional query options.

Options Hash (options):

  • :flags (Array)

    The flags for the update message.

    Supported flags: :upsert, :multi_update



54
55
56
57
58
59
60
61
62
63
# File 'lib/mongo/protocol/update.rb', line 54

def initialize(database, collection, selector, update, options = {})
  @database    = database
  @collection  = collection
  @namespace   = "#{database}.#{collection}"
  @selector    = selector
  @update      = update
  @flags       = options[:flags] || []
  @upconverter = Upconverter.new(collection, selector, update, flags)
  super
end

Instance Method Details

#payloadHash

Return the event payload for monitoring.

Examples:

Return the event payload.

message.payload

Returns:

  • (Hash)

    The event payload.

Since:

  • 2.1.0



73
74
75
76
77
78
79
80
# File 'lib/mongo/protocol/update.rb', line 73

def payload
  {
    command_name: 'update',
    database_name: @database,
    command: upconverter.command,
    request_id: request_id
  }
end