Class: Mongo::BulkWrite

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Retryable
Defined in:
lib/mongo/bulk_write.rb,
lib/mongo/bulk_write/result.rb,
lib/mongo/bulk_write/combineable.rb,
lib/mongo/bulk_write/validatable.rb,
lib/mongo/bulk_write/transformable.rb,
lib/mongo/bulk_write/result_combiner.rb,
lib/mongo/bulk_write/ordered_combiner.rb,
lib/mongo/bulk_write/unordered_combiner.rb

Defined Under Namespace

Modules: Combineable, Transformable, Validatable Classes: OrderedCombiner, Result, ResultCombiner, UnorderedCombiner

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Retryable

#read_with_one_retry, #read_with_retry, #write_with_retry

Constructor Details

#initialize(collection, requests, options = {}) ⇒ BulkWrite

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create the new bulk write operation.

Examples:

Create an ordered bulk write.

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

Create an unordered bulk write.

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

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 }}}
  ]
)

Parameters:

  • collection (Mongo::Collection)

    The collection.

  • requests (Array<Hash, BSON::Document>)

    The requests.

  • options (Hash, BSON::Document) (defaults to: {})

    The options.

Since:

  • 2.1.0



98
99
100
101
102
# File 'lib/mongo/bulk_write.rb', line 98

def initialize(collection, requests, options = {})
  @collection = collection
  @requests = requests
  @options = options || {}
end

Instance Attribute Details

#collectionMongo::Collection (readonly)

Returns collection The collection.

Returns:



29
30
31
# File 'lib/mongo/bulk_write.rb', line 29

def collection
  @collection
end

#optionsHash, BSON::Document (readonly)

Returns options The options.

Returns:

  • (Hash, BSON::Document)

    options The options.



35
36
37
# File 'lib/mongo/bulk_write.rb', line 35

def options
  @options
end

#requestsArray<Hash, BSON::Document> (readonly)

Returns requests The requests.

Returns:

  • (Array<Hash, BSON::Document>)

    requests The requests.



32
33
34
# File 'lib/mongo/bulk_write.rb', line 32

def requests
  @requests
end

Instance Method Details

#executeMongo::BulkWrite::Result

Execute the bulk write operation.

Examples:

Execute the bulk write.

bulk_write.execute

Returns:

Since:

  • 2.1.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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?true, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Is the bulk write ordered?

Examples:

Is the bulk write ordered?

bulk_write.ordered?

Returns:

  • (true, false)

    If the bulk write is ordered.

Since:

  • 2.1.0



114
115
116
# File 'lib/mongo/bulk_write.rb', line 114

def ordered?
  @ordered ||= options.fetch(:ordered, true)
end

#write_concernWriteConcern

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the write concern for the bulk write.

Examples:

Get the write concern.

bulk_write.write_concern

Returns:

Since:

  • 2.1.0



128
129
130
131
# 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