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.

@example Get the documents.

result.documents

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

@since 2.0.0

# File lib/mongo/operation/aggregate/result.rb, line 66
def documents
  reply.documents[0][RESULT] || explain_document ||
      cursor_document[FIRST_BATCH]
end

Private Instance Methods

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