BsonJavaScript
BsonJavaScript主要应用于mongodb驱动中
1.进行数据分组
MongoClient client = new MongoClient(host.ConnectionString); var server = client.GetServer(); var dataBase = server.GetDatabase(host.DatabaseName); var collectionClient = dataBase.GetCollection<TestEntity>(host.CollectionName);
//创建分组结果字典 Dictionary<string, double> dic_M = new Dictionary<string, double>(); dic_M["msum"] = 0.0;//分组数据统计值 //"Sex",进行分组的字段 //"function(doc,prev){if(doc.Age>25){ prev.msum++;}}" doc是个实体,获取年龄大于25岁的,进行统计
//"function(doc){ doc.count=doc.msum;delete doc.msum; }" 把分组数据统计值赋给字典values,并删除分组数据统计值
var result = collectionClient.Group(null, "Sex", BsonDocument.Create(dic_M), BsonJavaScript.Create("function(doc,prev){if(doc.Age>25){ prev.msum++;}}"),
BsonJavaScript.Create("function(doc){ doc.count=doc.msum;delete doc.msum; }")).ToList();