码迷,mamicode.com
首页 > 数据库 > 详细

C#操作mongodb(聚合函数)-分组找出每组的最大值

时间:2018-05-10 17:16:42      阅读:1208      评论:0      收藏:0      [点我收藏+]

标签:syn   max   ase   var   ==   element   分组   res   god   

 public static void OnQuery_QXData(string DBName, string tablename,string layername)
        {
            if (ConnectionString == null) OnCreateDB();
            MoDataBase = MoClient.GetDatabase(DBName);
            var collection = MoDataBase.GetCollection<BsonDocument>(tablename);           

            //{$group:{_id:{stationID:"$stationID",stationName:"$stationName"},TimeData:{$max:"$TimeData"},eValue:{$last:"$eValue"}}}           

            var group = new BsonDocument {

                {"_id",new BsonDocument
                {
                    {"stationID","$stationID"},
                    {"stationName","$stationName" }
                }
        },
                //{"stationID",new BsonDocument{ { "stationID", "$stationID" } } },
                {"stationID",new BsonDocument("$last","$stationID")},
                { "TimeData",new BsonDocument("$max","$TimeData")},
                {"eValue" ,new BsonDocument("$last","$eValue")}
            };
            var list = collection.Aggregate().Group(group).ToListAsync().Result;
            double value = 0.0;
            foreach (BsonDocument bsondoc in list)
            {
               
                double  evalue = double.TryParse(bsondoc.GetElement("eValue").Value.ToString(),out value)?value:0;
                string stationid = bsondoc.GetElement("stationID").Value.ToString();              
            }
        }
    }
}

C#操作mongodb(聚合函数)-分组找出每组的最大值

标签:syn   max   ase   var   ==   element   分组   res   god   

原文地址:https://www.cnblogs.com/wjr0117/p/9020092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!