123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Operation::MapReduce::Result

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Mongo::Operation::Result, Forwardable
Instance Chain:
self, ::Mongo::Operation::Result, Enumerable
Inherits: Mongo::Operation::Result
Defined in: lib/mongo/operation/map_reduce/result.rb

Overview

Defines custom behavior of results for a map reduce operation.

Since:

  • 2.0.0

Constant Summary

::Mongo::Operation::Result - Inherited

CURSOR, CURSOR_ID, FIRST_BATCH, N, NAMESPACE, NEXT_BATCH, OK, RESULT

Class Method Summary

::Mongo::Operation::Result - Inherited

.new

Initialize a new result.

Instance Attribute Summary

::Mongo::Operation::Result - Inherited

#acknowledged?

Is the result acknowledged?

#connection_description, #connection_global_id,
#has_cursor_id?

Whether the result contains cursor_id.

#ok?

Check the first document’s ok field.

#replies,
#successful?

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

#write_concern_error?

Whether the operation failed with a write concern error.

#query_failure?

Instance Method Summary

::Mongo::Operation::Result - Inherited

#cluster_time

Get the cluster time reported in the server response.

#cursor_id

Get the cursor id if the response is acknowledged.

#documents

Get the documents in the result.

#each

Iterate over the documents in the replies.

#error

The exception instance (of the ::Mongo::Error::OperationFailure class) that would be raised during processing of this result.

#inspect

Get the pretty formatted inspection of the result.

#labels

Gets the set of error labels associated with the result.

#n
#namespace

Get the namespace of the cursor.

#operation_time

Get the operation time reported in the server response.

#reply

Get the reply from the result.

#returned_count

Get the number of documents returned by the server in this batch.

#snapshot_timestamp, #topology_version,
#validate!

Validate the result by checking for any errors.

#written_count

Get the number of documents written by the server.

#aggregate_returned_count, #aggregate_written_count, #first_document, #parser,
#raise_operation_failure

Raises a Mongo::OperationFailure exception corresponding to the error information in this result.

Instance Attribute Details

#successful?true, false (readonly)

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

[ GitHub ]

  
# File 'lib/mongo/operation/map_reduce/result.rb', line 85

def successful?
  !documents.nil?
end

Instance Method Details

#countsHash

Gets the map/reduce counts from the reply.

Examples:

Get the counts.

result.counts

Returns:

  • (Hash)

    A hash of the result counts.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/operation/map_reduce/result.rb', line 55

def counts
  reply.documents[0][COUNTS]
end

#cursor_idInteger

This method is for internal use only.

Get the cursor id.

Examples:

Get the cursor id.

result.cursor_id

Returns:

  • (Integer)

    Always 0 because map reduce doesn’t return a cursor.

Since:

  • 2.5.0

[ GitHub ]

  
# File 'lib/mongo/operation/map_reduce/result.rb', line 130

def cursor_id
  0
end

#documentsArray<BSON::Document>

Get the documents from the map/reduce.

Examples:

Get the documents.

result.documents

Returns:

  • (Array<BSON::Document>)

    The documents.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/operation/map_reduce/result.rb', line 68

def documents
  reply.documents[0][RESULTS] || reply.documents[0][RESULT]
end

#first_document (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/operation/map_reduce/result.rb', line 152

def first_document
  @first_document ||= reply.documents[0]
end

#returned_countInteger

Get the number of documents returned by the server in this batch.

Map/Reduce operation returns documents inline without using cursors; as such, the standard Mongo::Reply#returned_count does not work correctly for Map/Reduce.

Note that the Map/Reduce operation is limited to max BSON document size (16 MB) in its inline result set.

Returns:

  • (Integer)

    The number of documents returned.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/operation/map_reduce/result.rb', line 146

def returned_count
  reply.documents.length
end

#timeInteger

Get the execution time of the map/reduce.

Examples:

Get the execution time.

result.time

Returns:

  • (Integer)

    The executing time in milliseconds.

Since:

  • 2.0.0

[ GitHub ]

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

def time
  reply.documents[0][TIME]
end

#validate!Result

This method is for internal use only.
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

[ GitHub ]

  
# File 'lib/mongo/operation/map_reduce/result.rb', line 117

def validate!
  documents.nil? ? raise_operation_failure : self
end