class Mongo::BulkWrite

Attributes

collection[R]

@return [ Mongo::Collection ] collection The collection.

options[R]

@return [ Hash, BSON::Document ] options The options.

requests[R]

@return [ Array<Hash, BSON::Document> ] requests The requests.

Public Class Methods

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

Create the new bulk write operation.

@api private

@example Create an ordered bulk write.

Mongo::BulkWrite.new(collection, [{ insert_one: { _id: 1 }}])

@example Create an unordered bulk write.

Mongo::BulkWrite.new(collection, [{ insert_one: { _id: 1 }}], ordered: false)

@example Create an ordered mixed bulk write.

Mongo::BulkWrite.new(
  collection,
  [
    { insert_one: { _id: 1 }},
    { update_one: { filter: { _id: 0 }, update: { '$set' => { name: 'test' }}}},
    { delete_one: { filter: { _id: 2 }}}
  ]
)

@param [ Mongo::Collection ] collection The collection. @param [ Array<Hash, BSON::Document> ] requests The requests. @param [ Hash, BSON::Document ] options The options.

@since 2.1.0

# File lib/mongo/bulk_write.rb, line 98
def initialize(collection, requests, options = {})
  @collection = collection
  @requests = requests
  @options = options || {}
end

Public Instance Methods

execute() click to toggle source

Execute the bulk write operation.

@example Execute the bulk write.

bulk_write.execute

@return [ Mongo::BulkWrite::Result ] The result.

@since 2.1.0

# File lib/mongo/bulk_write.rb, line 53
def execute
  operation_id = Monitoring.next_operation_id
  result_combiner = ResultCombiner.new
  write_with_retry do
    operations = op_combiner.combine
    server = next_primary
    raise Error::UnsupportedCollation.new if op_combiner.has_collation && !server.features.collation_enabled?
    operations.each do |operation|
      execute_operation(
        operation.keys.first,
        operation.values.first,
        server,
        operation_id,
        result_combiner
      )
    end
  end
  result_combiner.result
end
ordered?() click to toggle source

Is the bulk write ordered?

@api private

@example Is the bulk write ordered?

bulk_write.ordered?

@return [ true, false ] If the bulk write is ordered.

@since 2.1.0

# File lib/mongo/bulk_write.rb, line 114
def ordered?
  @ordered ||= options.fetch(:ordered, true)
end
write_concern() click to toggle source

Get the write concern for the bulk write.

@api private

@example Get the write concern.

bulk_write.write_concern

@return [ WriteConcern ] The write concern.

@since 2.1.0

# File lib/mongo/bulk_write.rb, line 128
def write_concern
  @write_concern ||= options[:write_concern] ?
    WriteConcern.get(options[:write_concern]) : collection.write_concern
end

Private Instance Methods

base_spec(operation_id) click to toggle source
# File lib/mongo/bulk_write.rb, line 135
def base_spec(operation_id)
  {
    :db_name => database.name,
    :coll_name => collection.name,
    :write_concern => write_concern,
    :ordered => ordered?,
    :operation_id => operation_id,
    :bypass_document_validation => !!options[:bypass_document_validation],
    :options => options,
    :id_generator => client.options[:id_generator]
  }
end
delete(documents, server, operation_id) click to toggle source
# File lib/mongo/bulk_write.rb, line 170
def delete(documents, server, operation_id)
  Operation::Write::Bulk::Delete.new(
    base_spec(operation_id).merge(:deletes => documents)
  ).execute(server)
end
Also aliased as: delete_one, delete_many
delete_many(documents, server, operation_id)
Alias for: delete
delete_one(documents, server, operation_id)
Alias for: delete
execute_operation(name, values, server, operation_id, combiner) click to toggle source
# File lib/mongo/bulk_write.rb, line 148
def execute_operation(name, values, server, operation_id, combiner)
  begin
    if values.size > server.max_write_batch_size
      split_execute(name, values, server, operation_id, combiner)
    else
      combiner.combine!(send(name, values, server, operation_id), values.size)
    end
  rescue Error::MaxBSONSize, Error::MaxMessageSize => e
    raise e if values.size <= 1
    split_execute(name, values, server, operation_id, combiner)
  end
end
insert_one(documents, server, operation_id) click to toggle source
# File lib/mongo/bulk_write.rb, line 179
def insert_one(documents, server, operation_id)
  Operation::Write::Bulk::Insert.new(
    base_spec(operation_id).merge(:documents => documents)
  ).execute(server)
end
op_combiner() click to toggle source
# File lib/mongo/bulk_write.rb, line 161
def op_combiner
  @op_combiner ||= ordered? ? OrderedCombiner.new(requests) : UnorderedCombiner.new(requests)
end
replace_one(documents, server, operation_id)
Alias for: update
split_execute(name, values, server, operation_id, combiner) click to toggle source
# File lib/mongo/bulk_write.rb, line 165
def split_execute(name, values, server, operation_id, combiner)
  execute_operation(name, values.shift(values.size / 2), server, operation_id, combiner)
  execute_operation(name, values, server, operation_id, combiner)
end
update(documents, server, operation_id) click to toggle source
# File lib/mongo/bulk_write.rb, line 185
def update(documents, server, operation_id)
  Operation::Write::Bulk::Update.new(
    base_spec(operation_id).merge(:updates => documents)
  ).execute(server)
end
Also aliased as: replace_one, update_one, update_many
update_many(documents, server, operation_id)
Alias for: update
update_one(documents, server, operation_id)
Alias for: update