Class: Moped::Protocol::Insert

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

Overview

The Protocol class for inserting documents into a collection.

Examples:

insert = Insert.new "moped", "people", [{ name: "John" }]

Continuing after an error on batch insert

insert = Insert.new "moped", "people",
  [{ unique_field: 1 }, { unique_field: 1 }, { unique_field: 2 }],
  flags: [:continue_on_error]

Setting the request id

insert = Insert.new "moped", "people", [{ name: "John" }],
  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, documents, options = {}) ⇒ Insert

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

Examples:

Insert.new "moped", "users", [{ name: "John" }],
  flags: [:continue_on_error],
  request_id: 123

Parameters:

  • database (String, Symbol)

    the database to insert into

  • collection (String, Symbol)

    the collection to insert into

  • documents (Array<Hash>)

    the documents to insert

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



78
79
80
81
82
83
84
85
86
# File 'lib/moped/protocol/insert.rb', line 78

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

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

Instance Attribute Details

#collectionString, Symbol (readonly)

Returns the collection this insert targets.

Returns:



61
62
63
# File 'lib/moped/protocol/insert.rb', line 61

def collection
  @collection
end

#databaseString, Symbol (readonly)

Returns the database this insert targets.

Returns:



58
59
60
# File 'lib/moped/protocol/insert.rb', line 58

def database
  @database
end

#documentsArray<Hash>

Returns the documents to insert.

Returns:



47
# File 'lib/moped/protocol/insert.rb', line 47

document :documents, type: :array

#flagsArray

The flags for the query. Supported flags are: :continue_on_error.

Parameters:

  • flags (Array)

    the flags for this message

Returns:

  • (Array)

    the flags for this message



39
# File 'lib/moped/protocol/insert.rb', line 39

flags    :flags, continue_on_error: 2 ** 0

#full_collection_nameString

Returns the namespaced collection name.

Returns:

  • (String)

    the namespaced collection name



43
# File 'lib/moped/protocol/insert.rb', line 43

cstring  :full_collection_name

#lengthNumber

Returns the length of the message.

Returns:

  • (Number)

    the length of the message



22
# File 'lib/moped/protocol/insert.rb', line 22

int32 :length

#op_codeNumber

Returns OP_INSERT operation code (2002).

Returns:

  • (Number)

    OP_INSERT operation code (2002)



32
# File 'lib/moped/protocol/insert.rb', line 32

int32 :op_code

#request_idNumber

Returns the request id of the message.

Returns:

  • (Number)

    the request id of the message



26
# File 'lib/moped/protocol/insert.rb', line 26

int32 :request_id

Instance Method Details

#log_inspectObject



88
89
90
91
92
# File 'lib/moped/protocol/insert.rb', line 88

def log_inspect
  type = "INSERT"

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