Class: Mongo::BulkWrite::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/bulk_write/result.rb

Overview

Wraps a series of bulk write operations in a result object.

Since:

  • 2.0.6

Constant Summary

REMOVED_COUNT =

Constant for number removed.

Since:

  • 2.1.0

'n_removed'.freeze
INSERTED_COUNT =

Constant for number inserted.

Since:

  • 2.1.0

'n_inserted'.freeze
INSERTED_IDS =

Constant for inserted ids.

Since:

  • 2.1.0

'inserted_ids'.freeze
MATCHED_COUNT =

Constant for number matched.

Since:

  • 2.1.0

'n_matched'.freeze
MODIFIED_COUNT =

Constant for number modified.

Since:

  • 2.1.0

'n_modified'.freeze
UPSERTED =

Constant for upserted.

Since:

  • 2.1.0

'upserted'.freeze
UPSERTED_COUNT =

Constant for number upserted.

Since:

  • 2.1.0

'n_upserted'.freeze
UPSERTED_IDS =

Constant for upserted ids.

Since:

  • 2.1.0

'upserted_ids'.freeze
FIELDS =

The fields contained in the result document returned from executing the operations.

Since:

  • 2.1.0.

[
  INSERTED_COUNT,
  REMOVED_COUNT,
  MODIFIED_COUNT,
  UPSERTED_COUNT,
  MATCHED_COUNT,
  Operation::Result::N
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(results) ⇒ Result

Create the new result object from the results document.

Examples:

Create the new result.

Result.new({ 'n_inserted' => 10 })

Parameters:

  • results (BSON::Document, Hash)

    The results document.

Since:

  • 2.1.0



96
97
98
# File 'lib/mongo/bulk_write/result.rb', line 96

def initialize(results)
  @results = results
end

Instance Method Details

#deleted_countInteger

Returns the number of documents deleted.

Examples:

Get the number of deleted documents.

result.deleted_count

Returns:

  • (Integer)

    The number deleted.

Since:

  • 2.1.0



84
85
86
# File 'lib/mongo/bulk_write/result.rb', line 84

def deleted_count
  @results[REMOVED_COUNT]
end

#inserted_countInteger

Returns the number of documents inserted.

Examples:

Get the number of inserted documents.

result.inserted_count

Returns:

  • (Integer)

    The number inserted.

Since:

  • 2.1.0



108
109
110
# File 'lib/mongo/bulk_write/result.rb', line 108

def inserted_count
  @results[INSERTED_COUNT]
end

#inserted_idsArray<BSON::ObjectId>

Get the inserted document ids, if the operation has inserts.

Examples:

Get the inserted ids.

result.inserted_ids

Returns:

  • (Array<BSON::ObjectId>)

    The inserted ids.

Since:

  • 2.1.0



120
121
122
# File 'lib/mongo/bulk_write/result.rb', line 120

def inserted_ids
  @results[INSERTED_IDS]
end

#matched_countInteger

Returns the number of documents matched.

Examples:

Get the number of matched documents.

result.matched_count

Returns:

  • (Integer)

    The number matched.

Since:

  • 2.1.0



132
133
134
# File 'lib/mongo/bulk_write/result.rb', line 132

def matched_count
  @results[MATCHED_COUNT]
end

#modified_countInteger

Returns the number of documents modified.

Examples:

Get the number of modified documents.

result.modified_count

Returns:

  • (Integer)

    The number modified.

Since:

  • 2.1.0



144
145
146
# File 'lib/mongo/bulk_write/result.rb', line 144

def modified_count
  @results[MODIFIED_COUNT]
end

#upserted_countInteger

Returns the number of documents upserted.

Examples:

Get the number of upserted documents.

result.upserted_count

Returns:

  • (Integer)

    The number upserted.

Since:

  • 2.1.0



156
157
158
# File 'lib/mongo/bulk_write/result.rb', line 156

def upserted_count
  @results[UPSERTED_COUNT]
end

#upserted_idsArray<BSON::ObjectId>

Get the upserted document ids, if the operation has inserts.

Examples:

Get the upserted ids.

result.upserted_ids

Returns:

  • (Array<BSON::ObjectId>)

    The upserted ids.

Since:

  • 2.1.0



168
169
170
# File 'lib/mongo/bulk_write/result.rb', line 168

def upserted_ids
  @results[UPSERTED_IDS] || []
end

#validate!Result

Validates the bulk write result.

Examples:

Validate the result.

result.validate!

Returns:

Raises:

Since:

  • 2.1.0



182
183
184
185
186
187
188
# File 'lib/mongo/bulk_write/result.rb', line 182

def validate!
  if @results[Error::WRITE_ERRORS] || @results[Error::WRITE_CONCERN_ERRORS]
    raise Error::BulkWriteError.new(@results)
  else
    self
  end
end