-
Type:
New Feature
-
Resolution: Fixed
-
Priority:
Minor - P4
-
Affects Version/s: 3.1.0
-
Component/s: Aggregation
-
Fully Compatible
-
Dotnet Drivers
-
Not Needed
-
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:
{{
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(),
}
)