mongoidaggregateaggregate-functionsmongoid5

MongoID 5 Aggregations: NoMethodError: undefined method `[]' for Aggregation


After upgrading to MongoID 5 I'm getting this error: NoMethodError: undefined method `[]' for #

The code looks like:

result = ::Presentation::Interaction.collection.aggregate(
  [
    user_match_criterias_live(conference),
    ::Presentation::ReportGenerator::DELCOUNTRY_AGGREGATION
  ]
)
return 0 if (result.count < 1)
return result[0]["total"]

So, aggregate is already using an array as an argument.


Solution

  • After checking what result contains, I realised that it doesn't respond to []. However, it does respond to .first

    The code would look like

    result = ::Presentation::Interaction.collection.aggregate(
      [
        user_match_criterias_live(conference),
        ::Presentation::ReportGenerator::DELCOUNTRY_AGGREGATION
      ]
    )
    return 0 if (result.count < 1)
    return result.first["total"]