class Mongo::Operation::Aggregate::Result

Defines custom behavior of results in an aggregation context.

@since 2.0.0

Constants

EXPLAIN

The field name for the aggregation explain information.

@since 2.0.5

EXPLAIN_LEGACY

The legacy field name for the aggregation explain information.

@since 2.0.5

Public Instance Methods

cursor_id() click to toggle source

Get the cursor id for the result.

@example Get the cursor id.

result.cursor_id

@note Even though the wire protocol has a cursor_id field for all

messages of type reply, it is always zero when using the
aggregation framework and must be retrieved from the cursor
document itself. Wahnsinn!

@return [ Integer ] The cursor id.

@since 2.0.0

# File lib/mongo/operation/aggregate/result.rb, line 51
def cursor_id
  cursor_document ? cursor_document[CURSOR_ID] : 0
end
documents() click to toggle source

Get the documents for the aggregation result. This is either the first document's 'result' field, or if a cursor option was selected, it is the 'firstBatch' field in the 'cursor' field of the first document returned. Otherwise, it is an explain document.

@example Get the documents.

result.documents

@return [ Array<BSON::Document> ] The documents.

@since 2.0.0

# File lib/mongo/operation/aggregate/result.rb, line 75
def documents
  docs = reply.documents[0][RESULT] 
  docs ||= cursor_document[FIRST_BATCH] if cursor_document
  docs ||= explain_document 
  docs
end
post_batch_resume_token() click to toggle source

Get the post batch resume token for the result

@return [ BSON::Document | nil ] The post batch resume token

@api private

# File lib/mongo/operation/aggregate/result.rb, line 60
def post_batch_resume_token
  cursor_document ? cursor_document['postBatchResumeToken'] : nil
end

Private Instance Methods

cursor_document() click to toggle source
# File lib/mongo/operation/aggregate/result.rb, line 90
def cursor_document
  @cursor_document ||= reply.documents[0][CURSOR]
end
explain_document() click to toggle source

This should only be called on explain responses; it will never return a nil result and will only be meaningful on explain responses

# File lib/mongo/operation/aggregate/result.rb, line 86
def explain_document
  first_document[EXPLAIN] || first_document[EXPLAIN_LEGACY] || [first_document]
end
first_document() click to toggle source
# File lib/mongo/operation/aggregate/result.rb, line 94
def first_document
  @first_document ||= reply.documents[0]
end