Class: Mongo::Operation::Result

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/mongo/operation/result.rb,
lib/mongo/operation/shared/result/aggregatable.rb

Overview

Result wrapper for operations.

Since:

  • 2.0.0

Defined Under Namespace

Modules: Aggregatable

Constant Summary collapse

CURSOR =

The field name for the cursor document in an aggregation.

Since:

  • 2.2.0

'cursor'.freeze
CURSOR_ID =

The cursor id field in the cursor document.

Since:

  • 2.2.0

'id'.freeze
FIRST_BATCH =

The field name for the first batch of a cursor.

Since:

  • 2.2.0

'firstBatch'.freeze
NEXT_BATCH =

The field name for the next batch of a cursor.

Since:

  • 2.2.0

'nextBatch'.freeze
NAMESPACE =

The namespace field in the cursor document.

Since:

  • 2.2.0

'ns'.freeze
N =

The number of documents updated in the write.

Since:

  • 2.0.0

'n'.freeze
OK =

The ok status field in the result.

Since:

  • 2.0.0

'ok'.freeze
RESULT =

The result field constant.

Since:

  • 2.2.0

'result'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(replies) ⇒ Result

Initialize a new result.

Examples:

Instantiate the result.

Result.new(replies)

Parameters:

Since:

  • 2.0.0



75
76
77
# File 'lib/mongo/operation/result.rb', line 75

def initialize(replies)
  @replies = [ *replies ] if replies
end

Instance Attribute Details

#repliesArray<Protocol::Reply> (readonly)

Returns replies The wrapped wire protocol replies.

Returns:

Since:

  • 2.0.0



80
81
82
# File 'lib/mongo/operation/result.rb', line 80

def replies
  @replies
end

Instance Method Details

#acknowledged?true, false

Note:

On MongoDB 2.6 and higher all writes are acknowledged since the driver uses write commands for all write operations. On 2.4 and lower, the result is acknowledged if the GLE has been executed after the command. If not, no replies will be specified. Reads will always return true here since a replies is always provided.

Is the result acknowledged?

Returns:

  • (true, false)

    If the result is acknowledged.

Since:

  • 2.0.0



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

def acknowledged?
  !!@replies
end

#cluster_timeBSON::Document

Get the cluster time reported in the server response.

Examples:

Get the cluster time.

result.cluster_time

Returns:

  • (BSON::Document)

    The cluster time document.

Since:

  • 2.5.0



317
318
319
# File 'lib/mongo/operation/result.rb', line 317

def cluster_time
  first_document && first_document[CLUSTER_TIME]
end

#cursor_idInteger

Note:

Cursor ids of 0 indicate there is no cursor on the server.

Get the cursor id if the response is acknowledged.

Examples:

Get the cursor id.

result.cursor_id

Returns:

  • (Integer)

    The cursor id.

Since:

  • 2.0.0



123
124
125
# File 'lib/mongo/operation/result.rb', line 123

def cursor_id
  acknowledged? ? replies.last.cursor_id : 0
end

#documentsArray<BSON::Document>

Get the documents in the result.

Examples:

Get the documents.

result.documents

Returns:

  • (Array<BSON::Document>)

    The documents.

Since:

  • 2.0.0



145
146
147
148
149
150
151
# File 'lib/mongo/operation/result.rb', line 145

def documents
  if acknowledged?
    replies.flat_map{ |reply| reply.documents }
  else
    []
  end
end

#each {|Each| ... } ⇒ Enumerator

Iterate over the documents in the replies.

Examples:

Iterate over the documents.

result.each do |doc|
  p doc
end

Yield Parameters:

  • Each (BSON::Document)

    document in the result.

Returns:

  • (Enumerator)

    The enumerator.

Since:

  • 2.0.0



165
166
167
# File 'lib/mongo/operation/result.rb', line 165

def each(&block)
  documents.each(&block)
end

#inspectString

Get the pretty formatted inspection of the result.

Examples:

Inspect the result.

result.inspect

Returns:

  • (String)

    The inspection.

Since:

  • 2.0.0



177
178
179
# File 'lib/mongo/operation/result.rb', line 177

def inspect
  "#<#{self.class.name}:0x#{object_id} documents=#{documents}>"
end

#labelsArray

Gets the set of error labels associated with the result.

Examples:

Get the labels.

result.labels

Returns:

  • (Array)

    labels The set of labels.

Since:

  • 2.7.0



329
330
331
# File 'lib/mongo/operation/result.rb', line 329

def labels
  @labels ||= parser.labels
end

#multiple?true, false

Determine if this result is a collection of multiple replies from the server.

Examples:

Is the result for multiple replies?

result.multiple?

Returns:

  • (true, false)

    If the result is for multiple replies.

Since:

  • 2.0.0



109
110
111
# File 'lib/mongo/operation/result.rb', line 109

def multiple?
  replies.size > 1
end

#namespaceNil

Get the namespace of the cursor. The method should be defined in result classes where 'ns' is in the server response.

Returns:

  • (Nil)

Since:

  • 2.0.0



133
134
135
# File 'lib/mongo/operation/result.rb', line 133

def namespace
  nil
end

#ok?true, false

Check the first document's ok field.

Examples:

Check the ok field.

result.ok?

Returns:

  • (true, false)

    If the command returned ok.

Since:

  • 2.1.0



242
243
244
245
246
247
248
249
# File 'lib/mongo/operation/result.rb', line 242

def ok?
  # first_document[OK] is a float, and the server can return
  # ok as a BSON int32, BSON int64 or a BSON double.
  # The number 1 is exactly representable in a float, hence
  # 1.0 == 1 is going to perform correctly all of the time
  # (until the server returns something other than 1 for success, that is)
  first_document[OK] == 1
end

#operation_timeObject

Get the operation time reported in the server response.

Examples:

Get the operation time.

result.operation_time

Returns:

  • (Object)

    The operation time value.

Since:

  • 2.5.0



305
306
307
# File 'lib/mongo/operation/result.rb', line 305

def operation_time
  first_document && first_document[OPERATION_TIME]
end

#replyProtocol::Reply

Get the first reply from the result.

Examples:

Get the first reply.

result.reply

Returns:

Since:

  • 2.0.0



189
190
191
192
193
194
195
# File 'lib/mongo/operation/result.rb', line 189

def reply
  if acknowledged?
    replies.first
  else
    nil
  end
end

#returned_countInteger

Get the count of documents returned by the server.

Examples:

Get the number returned.

result.returned_count

Returns:

  • (Integer)

    The number of documents returned.

Since:

  • 2.0.0



205
206
207
208
209
210
211
# File 'lib/mongo/operation/result.rb', line 205

def returned_count
  if acknowledged?
    multiple? ? aggregate_returned_count : reply.number_returned
  else
    0
  end
end

#successful?true, false

Note:

If the write was unacknowledged, then this will always return true.

If the result was a command then determine if it was considered a success.

Examples:

Was the command successful?

result.successful?

Returns:

  • (true, false)

    If the command was successful.

Since:

  • 2.0.0



225
226
227
228
229
230
231
232
# File 'lib/mongo/operation/result.rb', line 225

def successful?
  return true if !acknowledged?
  if first_document.has_key?(OK)
    ok? && parser.message.empty?
  else
    !query_failure? && parser.message.empty?
  end
end

#validate!Result

Note:

This only checks for errors with writes since authentication is handled at the connection level and any authentication errors would be raised there, before a Result is ever created.

Validate the result by checking for any errors.

Examples:

Validate the result.

result.validate!

Returns:

  • (Result)

    The result if verification passed.

Raises:

Since:

  • 2.0.0



265
266
267
# File 'lib/mongo/operation/result.rb', line 265

def validate!
  !successful? ? raise_operation_failure : self
end

#written_countInteger Also known as: n

Get the number of documents written by the server.

Examples:

Get the number of documents written.

result.written_count

Returns:

  • (Integer)

    The number of documents written.

Since:

  • 2.0.0



288
289
290
291
292
293
294
# File 'lib/mongo/operation/result.rb', line 288

def written_count
  if acknowledged?
    multiple? ? aggregate_written_count : (first_document[N] || 0)
  else
    0
  end
end