Class: Moped::Protocol::Update

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

Overview

The Protocol class for updating documents in a collection.

Examples:

Rename a user

Update.new "moped", "users", { _id: "123" }, { name: "Bob" }

Rename all users named John

Update.new "moped", "users", { name: "John" }, { name: "Bob" },
  flags: [:multi]

Upsert

Update.new "moped", "users", { name: "John" }, { name: "John" },
  flags: [:upsert]

Setting the request id

Update.new "moped", "users", {}, { name: "Bob" },
  request_id: 123

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Message

included, #inspect, #receive_replies, #serialize

Constructor Details

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

Create a new update command. The database and collection arguments are joined together to set the full_collection_name.

Examples:

Update.new "moped", "users", { name: "John" }, { name: "Bob" },
  flags: [:upsert],
  request_id: 123

Parameters:

  • database (String, Symbol)

    the database to insert into

  • collection (String, Symbol)

    the collection to insert into

  • selector (Hash)

    the selector

  • update (Hash)

    the update to perform

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

    additional options

Options Hash (options):

  • :request_id (Number)

    the command’s request id

  • :flags (Array)

    the flags for insertion. Supported flags: :upsert, :multi.



89
90
91
92
93
94
95
96
97
98
# File 'lib/moped/protocol/update.rb', line 89

def initialize(database, collection, selector, update, options = {})
  @database = database
  @collection = collection

  @full_collection_name = "#{database}.#{collection}"
  @selector             = selector
  @update               = update
  @request_id           = options[:request_id]
  @flags                = options[:flags]
end

Instance Attribute Details

#collectionString, Symbol (readonly)

Returns the collection this insert targets.

Returns:



71
72
73
# File 'lib/moped/protocol/update.rb', line 71

def collection
  @collection
end

#databaseString, Symbol (readonly)

Returns the database this insert targets.

Returns:



68
69
70
# File 'lib/moped/protocol/update.rb', line 68

def database
  @database
end

#flagsArray

The flags for the update message. Supported flags are :upsert and :multi.

Parameters:

  • flags (Array)

    the flags for this message

Returns:

  • (Array)

    the flags for this message



48
49
# File 'lib/moped/protocol/update.rb', line 48

flags    :flags, upsert: 2 ** 0,
multi:  2 ** 1

#full_collection_nameString

Returns the namespaced collection name.

Returns:

  • (String)

    the namespaced collection name



41
# File 'lib/moped/protocol/update.rb', line 41

cstring  :full_collection_name

#lengthNumber

Returns the length of the message.

Returns:

  • (Number)

    the length of the message



25
# File 'lib/moped/protocol/update.rb', line 25

int32 :length

#op_codeNumber

Returns OP_UPDATE operation code (2001).

Returns:

  • (Number)

    OP_UPDATE operation code (2001)



35
# File 'lib/moped/protocol/update.rb', line 35

int32 :op_code

#request_idNumber

Returns the request id of the message.

Returns:

  • (Number)

    the request id of the message



29
# File 'lib/moped/protocol/update.rb', line 29

int32 :request_id

#selectorHash

Returns the selector for the update.

Returns:

  • (Hash)

    the selector for the update



53
# File 'lib/moped/protocol/update.rb', line 53

document :selector

#updateHash

Returns the updates to apply.

Returns:

  • (Hash)

    the updates to apply



57
# File 'lib/moped/protocol/update.rb', line 57

document :update

Instance Method Details

#log_inspectObject



100
101
102
103
104
105
106
107
# File 'lib/moped/protocol/update.rb', line 100

def log_inspect
  type = "UPDATE"

  "%-12s database=%s collection=%s selector=%s update=%s flags=%s" % [type, database, collection,
                                                                      selector.inspect,
                                                                      update.inspect,
                                                                      flags.inspect]
end