asp.net - MongoDB Driver C# trying to do basic math but don't understand aggregation -


i have collection of bsondocuments each have field named "engagement" (which number) , field named "color". total sum of cost bsondocuments "color" = "blue".

so far i've found need this:

            var collection = db.getcollection<bsondocument>("collectionname");              var match = new bsondocument             {                 {                     "$match",                     new bsondocument                         {                             {"sentiment", "positive"}                         }                 }             };              var group = new bsondocument             {                 { "$group",                     new bsondocument                         {                             {                                 "sum", new bsondocument                                              {                                                  {                                                      "$sum", "$engagement"                                                  }                                              }                             }                         }               }             };              var pipeline = new[] { match, group };             var result = collection.aggregate(pipeline); 

i've been getting error type methods arguments aggregate method cannot inferred usage.

simply put, know how perform simple math operations the aggregate framework. in example, trying sum values of engagement field of bsondocuments in collection. utilization of builder functions great if possible manually created bsondocument serve well.

thanks time

when use $group, need tell aggregation framework by fields want group. in _id field group-object. also, if want simple document count, need use { $sum : 1 } count 1 per document. if use field-name, tell aggregation interpret values of field numbers , add them up.

in mongodb shell syntax:

{ $group: {      _id: "$engagement",      sum: { $sum: 1 } }} 

translated c# (untested!):

{ "$group", new bsondocument      {          { "_id", "$engagement" },          { "sum", new bsondocument              {                  {                      "$sum", 1                  }               }          }      }  }  

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -