Support aggregation methods Eloquent\Model::withExists withCount withSum withAvg withMin withMax

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Fixed
    • Priority: Minor - P4
    • 5.10.0
    • Affects Version/s: None
    • None
    • 8
    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature?

      Aggregate methods on relationship have been implemented. They use a 2nd query to compute the aggregation. $lookup was not possible because we would have to convert all the queries to $match stage (high risk of breaking change).

      While this feature works for simple use-cases, it's recommended denormalize the aggregated data into the main document. (ie: store "comment_count" in a "articles" collection with a relation to many "comments").

      Explicit $lookup aggregation can also be used.

      2. Would you like the user to see examples of the syntax and/or executable code and its output?

      See tests: https://github.com/mongodb/laravel-mongodb/blob/5.x/tests/Eloquent/WithAggregateTest.php

      And Laravel Docs: https://laravel.com/docs/13.x/eloquent-relationships#aggregating-related-models 

      3. Which versions of the driver/connector does this apply to?

      Laravel-MongoDB 5.10+

      Show
      1. What would you like to communicate to the user about this feature? Aggregate methods on relationship have been implemented. They use a 2nd query to compute the aggregation. $lookup was not possible because we would have to convert all the queries to $match stage (high risk of breaking change). While this feature works for simple use-cases, it's recommended denormalize the aggregated data into the main document. (ie: store "comment_count" in a "articles" collection with a relation to many "comments"). Explicit $lookup aggregation can also be used. 2. Would you like the user to see examples of the syntax and/or executable code and its output? See tests: https://github.com/mongodb/laravel-mongodb/blob/5.x/tests/Eloquent/WithAggregateTest.php And Laravel Docs: https://laravel.com/docs/13.x/eloquent-relationships#aggregating-related-models   3. Which versions of the driver/connector does this apply to? Laravel-MongoDB 5.10+
    • None
    • None
    • None
    • None
    • None
    • None

            Assignee:
            Jérôme Tamarelle
            Reporter:
            Jérôme Tamarelle
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: