Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb group aggregation using ProjectionDefinition with c# driver

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

like image 574
Aaron Newman Avatar asked Dec 21 '16 22:12

Aaron Newman


People also ask

How can you group by a particular value in MongoDB?

We can group by single as well as multiple field from the collection, we can use $group operator in MongoDB to group fields from the collection and returns the new document as result. We are using $avg, $sum, $max, $min, $push, $last, $first and $addToSet operator with group by in MongoDB.

Is MongoDB aggregation fast?

On large collections of millions of documents, MongoDB's aggregation was shown to be much worse than Elasticsearch. Performance worsens with collection size when MongoDB starts using the disk due to limited system RAM. The $lookup stage used without indexes can be very slow.

Can we use multiple group in MongoDB?

Mongodb group by multiple fields using Aggregate operation First, the key on which the grouping is based is selected and then the collection is divided into groups according to the selected key value. You can then create a final document by aggregating the documents in each group.


1 Answers

There are some great examples on AggregateGroupTranslatorTests.cs, which is the test file for the MongoDB .NET Driver: aggregation group.

For example, if you have this class definition:

public class ExampleGroup 
{
    [BsonId]
    public ObjectId Id {get;set;}
    public string SomeStringField { get; set; }
    public int SomeNumberField {get; set;}
}

And you would like to perform grouping of

{ _id: "$SomeStringField", Result: { "$max": "$SomeNumberField" } }

You could execute as below:

var result = collection.Aggregate()
                        .Group(
                            x => x.SomeStringField,
                            g => new {
                                  Result = g.Select(
                                           x => x.SomeNumberField
                                           ).Max()
                            }
                        ).ToList();
result.ForEach(doc => Console.WriteLine(doc.ToJson()));

The above snippet was tested on .Net 4.5, MongoDB 3.4 and MongoDB .Net/C# Driver v2.3. I know you're using v2.2, but there's no (or any) changes for the project definition grouping.

Also check out other LINQ translator tests MongoDB.Driver.Tests/Linq/Translators

like image 96
Wan Bachtiar Avatar answered Sep 28 '22 02:09

Wan Bachtiar