Consider the following piece of code:
var results = collection.Aggregate()
...
.Lookup( ... )
.Project( ??? );
I need to call Project() on the above query. I haven't been able to figure out how to build a projection definition of type ProjectionDefinition<T1, T2>
, which is what Project() requires.
The Builders class doesn't seem to work in this case:
var projection = Builders<Event>.Projection.Include(x => x).Include("agg_res.SomeField");
as it instantiates a definition of type ProjectionDefinition<T>
.
I found the answer. An aggregation can perform a lookup and projection at the same time by using a different overload of Lookup():
var results = collection.Aggregate()
.Match(filter)
.Lookup<Event, User, AggregatedEvent>(users, e => e.OwnerId, u => u.Id, r => r.OwnerUsers)
.ToList();
This allows one to use lambdas to indicate which fields should be matched and where to place the join results (OwnerUsers in the above example). Note that AggregatedEvent extends Event and includes an array field called OwnerUsers. This is where the matches are placed.