Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New (2.6) $cond Aggregation Framework with c#?

I'm going crazy with this one...

I have this aggregation framework expression working like a charm in mongo shell:

{ $group : 
    {
    _id : '$Code' , 
    'Special' : { $sum : { $cond: [{ $eq: [ '$Special', 'Success']},1,0]}}
    }
}

I need to do it in c#, I tried a lot of combinations but without success.

Has anyone any clue?

Thx

like image 537
Ivan Fioravanti Avatar asked Oct 24 '25 18:10

Ivan Fioravanti


1 Answers

Give this a try:

var group = new BsonDocument
{
    {
        "$group",
        new BsonDocument
        {
            {
                "_id", "$Code"
            },
            {
                "Special", new BsonDocument
                {
                    { "$sum", new BsonDocument
                        {
                            {"$cond", new BsonArray
                                {
                                    new BsonDocument
                                    {
                                        {
                                            "$eq", new BsonArray {"$Special", "Success"}
                                        }
                                    },
                                    1,
                                    0
                                }
                            }
                        }
                    }
                }
            }
        }
    }
};
like image 115
ajaybee Avatar answered Oct 28 '25 03:10

ajaybee