class Mongo::Operation::Commands::Aggregate::Result

Defines custom behaviour 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

Calls superclass method Mongo::Operation::Result#cursor_id
# File lib/mongo/operation/commands/aggregate/result.rb, line 52
def cursor_id
  cursor_document ? cursor_document[CURSOR_ID] : super
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/commands/aggregate/result.rb, line 67
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/commands/aggregate/result.rb, line 78
def cursor_document
  @cursor_document ||= reply.documents[0][CURSOR]
end
explain_document() click to toggle source
# File lib/mongo/operation/commands/aggregate/result.rb, line 74
def explain_document
  first_document[EXPLAIN] || first_document[EXPLAIN_LEGACY]
end
first_document() click to toggle source
# File lib/mongo/operation/commands/aggregate/result.rb, line 82
def first_document
  @first_document ||= reply.documents[0]
end