class Mongo::Protocol::Insert

MongoDB Wire protocol Insert message.

This is a client request message that is sent to the server in order to insert documents within a namespace.

The operation only has one flag :continue_on_error which the user can use to instruct the database server to continue processing a bulk insertion if one happens to fail (e.g. due to duplicate IDs). This makes builk insert behave similarly to a seires of single inserts, except lastError will be set if any insert fails, not just the last one.

If multiple errors occur, only the most recent will be reported by the getLastError mechanism.

@api semipublic

Constants

FLAGS

Available flags for an Insert message.

OP_CODE

The operation code required to specify an Insert message. @return [Fixnum] the operation code.

@since 2.5.0

Attributes

upconverter[R]

Public Class Methods

new(database, collection, documents, options = {}) click to toggle source

Creates a new Insert message

@example Insert a user document

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

@example Insert serveral user documents and continue on errors

Insert.new('xgen', 'users', users, :flags => [:continue_on_error])

@param database [String, Symbol] The database to insert into. @param collection [String, Symbol] The collection to insert into. @param documents [Array<Hash>] The documents to insert. @param options [Hash] Additional options for the insertion.

@option options :flags [Array] The flags for the insertion message.

Supported flags: +:continue_on_error+
Calls superclass method
# File lib/mongo/protocol/insert.rb, line 51
def initialize(database, collection, documents, options = {})
  @database = database
  @namespace = "#{database}.#{collection}"
  @documents = documents
  @flags = options[:flags] || []
  @upconverter = Upconverter.new(collection, documents, options)
  @options = options
  super
end

Public Instance Methods

payload() click to toggle source

Return the event payload for monitoring.

@example Return the event payload.

message.payload

@return [ BSON::Document ] The event payload.

@since 2.1.0

# File lib/mongo/protocol/insert.rb, line 69
def payload
  BSON::Document.new(
    command_name: 'insert',
    database_name: @database,
    command: upconverter.command,
    request_id: request_id
  )
end

Private Instance Methods

validating_keys?() click to toggle source
# File lib/mongo/protocol/insert.rb, line 84
def validating_keys?
  @options.fetch(:validating_keys, true)
end