I am trying to use the 2.2 version of the driver to create an aggregation query using the FilterDefintion for match phase and ProjectionDefinition for group phase. But I'm not quite sure how to create a ProjectionDefinition. The API is like:
FilterDefinition<T> filter=Builders<T>.Filter.Eq("Foo","Bar");
ProjectionDefinition<T> projection=...
IAggregateFluent<T> aggr = fileCol.Aggregate<T>()
.Match(filter)
.Group(projection);
The match filter works just like in a normal Find. But I'm not sure how to create the projection.
I can create just a normal Bson document and put it in that phase, and it works. But I'm trying to create a consistent interface using the strongly-typed objects that come from builders, and this is the one place where I can't figure out how to do that. I would think it is possible since the API exists.
(The Bson document for the group phase can be made like):
projection = new BsonDocument("_id","$SomeIdField").
Add("Result",new BsonDocument("$max","$someNumberField"));
EDIT: The MongoDB API I am referring to is linked below, and I quoted the relevant section. There are no examples provided. I agree that a 'GroupDefinition' would have made more sense, but I didn't write it :-) And by strongly typed, I mean typed with the return value of whatever is, not BsonDocument.
http://api.mongodb.com/csharp/current/html/M_MongoDB_Driver_AggregateFluentBase_1_Group__1.htm
Blockquote
public abstract IAggregateFluent<TNewResult> Group<TNewResult>(
ProjectionDefinition<TResult, TNewResult> group
)
Parameters
group Type: MongoDB.Driver.ProjectionDefinition<TResult, TNewResult>
The group projection.
Type Parameters
TNewResult
The type of the result of the stage.
Blockquote
ProjectionDefinitionin the grouping stage ?? that doesn't sound right. Its a stage on its own just like match. btwBsonDocumentare strongly typed. If its just a key value pair theBsonDocumentis your object and I think for the accumulator operators you already have the wrappers that you can use.