Class: Mongo::BulkWrite

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Operation::ResponseHandling
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

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

Raises:

Since:

  • 2.1.0

Parameters:

  • The collection.

  • The requests, cannot be empty.

  • (defaults to: {})

    The options.

API:

  • private



129
130
131
132
133
134
135
136
# File 'lib/mongo/bulk_write.rb', line 129

def initialize(collection, requests, options = {})
  @collection = collection
  @requests = requests
  @options = options || {}
  return unless @options[:timeout_ms] && @options[:timeout_ms] < 0

  raise ArgumentError, 'timeout_ms options must be non-negative integer'
end

Instance Attribute Details

#collectionMongo::Collection (readonly)

Returns collection The collection.

Returns:

  • collection The collection.



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

def collection
  @collection
end

#optionsHash, BSON::Document (readonly)

Returns options The options.

Returns:

  • options The options.



37
38
39
# File 'lib/mongo/bulk_write.rb', line 37

def options
  @options
end

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

Returns requests The requests.

Returns:

  • requests The requests.



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

def requests
  @requests
end

Instance Method Details

#executeMongo::BulkWrite::Result

Execute the bulk write operation.

Examples:

Execute the bulk write.

bulk_write.execute

Since:

  • 2.1.0

Returns:

  • The result.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mongo/bulk_write.rb', line 57

def execute
  operation_id = Monitoring.next_operation_id
  result_combiner = ResultCombiner.new
  operations = op_combiner.combine
  validate_requests!
  deadline = calculate_deadline

  client.with_session(@options) do |session|
    operations.each do |operation|
      context = Operation::Context.new(
        client: client,
        session: session,
        operation_timeouts: operation_timeouts(deadline)
      )
      if single_statement?(operation)
        write_concern = write_concern(session)
        write_with_retry(write_concern, context: context) do |connection, txn_num, context|
          execute_operation(
            operation.keys.first,
            operation.values.flatten,
            connection,
            context,
            operation_id,
            result_combiner,
            session,
            txn_num
          )
        end
      else
        nro_write_with_retry(write_concern, context: context) do |connection, _txn_num, context|
          execute_operation(
            operation.keys.first,
            operation.values.flatten,
            connection,
            context,
            operation_id,
            result_combiner,
            session
          )
        end
      end
    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?

Since:

  • 2.1.0

Returns:

  • If the bulk write is ordered.

API:

  • private



148
149
150
# File 'lib/mongo/bulk_write.rb', line 148

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

#write_concern(session = nil) ⇒ WriteConcern

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

Since:

  • 2.1.0

Returns:

  • The write concern.

API:

  • private



162
163
164
165
166
167
168
# File 'lib/mongo/bulk_write.rb', line 162

def write_concern(session = nil)
  @write_concern ||= if options[:write_concern]
                       WriteConcern.get(options[:write_concern])
                     else
                       collection.write_concern_with_session(session)
                     end
end