阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MongoDB在CSharp里实现Aggregate

101次阅读
没有评论

共计 2863 个字符,预计需要花费 8 分钟才能阅读完成。

今天主要用了一个 mongodb.driver 里的分组,事实上在网上介绍这方面的文章非常少,以至于我在出现问题后,无法找到一个正确的解决方案,最后还是通过异常信息找到的解决方法,所以感觉自己更应该去写一篇关于如何在C#驱动里进行聚合 Aggregate 的文章!

/// <summary>
        /// 返回 UI 消息树
        /// </summary>
        /// <returns></returns>
        public static string GetMongoLog(DateTime? fromDate, DateTime? toDate, int page = 1)
        {
            string from = DateTime.Now.Date.ToString(“yyyy-MM-dd”);
            string to = DateTime.Now.Date.AddDays(1).ToString(“yyyy-MM-dd”);
            if (fromDate.HasValue)
            {
                from = fromDate.Value.ToString(“yyyy-MM-dd”);

            }
            if (toDate.HasValue)
            {
                to = toDate.Value.ToString(“yyyy-MM-dd”);
            }
            var stages = new List<IPipelineStageDefinition>();
            stages.Add(new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(“{$match:{AddTime:{$gt:ISODate(‘” + from + “‘),$lt:ISODate(‘” + to + “‘)}}}”));
            stages.Add(new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(“{$group:{_id: \”$RootId\”, count: {$sum: 1}}}”));
            stages.Add(new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(“{$skip:” + page * 5 + “}”));
            stages.Add(new JsonPipelineStageDefinition<BsonDocument, BsonDocument>(“{$limit:5}”));
            var pipeline = new PipelineStagePipelineDefinition<BsonDocument, BsonDocument>(stages);
            var result = NoSql.MongodbManager<LoggerContext>.Collection.Aggregate(pipeline);
            StringBuilder str = new StringBuilder();

            str.Append(“<ol class=’treeMsg’>”);
            foreach (var item in result.ToList())
            {
                var timer = new List<DateTime>();
                var old = NoSql.MongodbManager<LoggerContext>.Instance.Find(i => i.RootId == item.Values.ToArray()[0].ToString() && i.ParentId == null).FirstOrDefault();
                timer.Add(old.AddTime);
                str.Append(“<li style=’margin:5px;border:1px dashed #aaa’>”);
                str.AppendFormat(“<span style=’color:red;’>{0}</span><span style=’color:green’>{1}</span><span>{2}</span>”
                  , old.Url
                  , old.MessageBody
                  , old.AddTime);
                MsgTree(str, old.ChildId, timer);
                str.AppendFormat(“<p><b><em> 本次请求用时 {0} 毫秒({1}秒)<em></b></p>”
                    , (timer.Max() – timer.Min()).TotalMilliseconds
                    , (timer.Max() – timer.Min()).TotalSeconds);
                str.Append(“</li>”);
            }
            str.Append(“</ol>”);
            return str.ToString();
        }

注意,目前 mongodb for C# 这个驱动,在进行 Aggregate 时,只支持 BsonDocument 类型,也就是说,你的集合 collection 也必须返回的是 BsonDocument,而实体类型是不可以被认出的,这点要注意.

MongoDB 在 CSharp 里实现 Aggregate

也正是如此,所以我们的 mongo 封装时,别忘记公开一个 BsonDocument 的对象供聚合使用!

MongoDB 在 CSharp 里实现 Aggregate

感谢各位的阅读,希望文章可以帮助大家!

更多 MongoDB 相关教程见以下内容

MongoDB 文档、集合、数据库简介  http://www.linuxidc.com/Linux/2016-12/138529.htm

MongoDB 3 分片部署及故障模拟验证  http://www.linuxidc.com/Linux/2016-12/138529.htm

Linux CentOS 6.5 yum 安装 MongoDB  http://www.linuxidc.com/Linux/2016-12/137790.htm

CentOS 7 yum 方式快速安装 MongoDB  http://www.linuxidc.com/Linux/2016-11/137679.htm

MongoDB 的查询操作  http://www.linuxidc.com/Linux/2016-10/136581.htm

MongoDB 权威指南第 2 版 PDF 完整带书签目录 下载见 http://www.linuxidc.com/Linux/2016-12/138253.htm

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-05/143854.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计2863字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中