Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-5446

Aggregate Group(...) does not support $addToSet

    • Type: Icon: New Feature New Feature
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 3.2.0
    • Affects Version/s: 3.1.0
    • Component/s: Aggregation
    • Fully Compatible
    • Dotnet Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      At the moment with MongoDb.Drive .Aggregate().Group(...) is impossible to use the $AddToSet operator. Looking at the code it seems that in the Group Translator is Hardcoded the $push operator.
      I see that in 2015 that problem was solved using HashSet, but in the 3.1.0 is translated in 2 steps:
      {{

      { $ push }

      followed by { $setUnion }}}
      That can generate an out of memory on the $push operation (with a lot of non unique records) that is not generated with the $addToSet.

      I think that the best way to solve that is to create a IEnumerable<T> extension called AddToSet(...) that is allowed by MongoDb translator.

      Example:

      public static IEnumerable<TResult> AddToSet<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
      {{{}}
          return source.Select(selector).Distinct();
      }

      that is implemented like:

      .Group(
          g => g.ImmobileCliente,
          grp => new MyObj()
          {{{}}
              MyProp = grp.AddToSet(i => i.MyProp).ToList(),
          }
      )

            Assignee:
            oleksandr.poliakov@mongodb.com Oleksandr Poliakov
            Reporter:
            a.valtorta@geniusanalytics.it Alessandro Valtorta
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: