MongoDB使用Aggregate进行数据分组

之前有一篇关于MongoDB的博客,但实际使用中数据量过大时速度比较慢,并且使用中不是很灵活。

 

第一 创建 MongoDBHelper类,封装MongoDB相关的操作

/// <summary>
        /// 数量
        /// </summary>
        /// <typeparam name="TDoc"></typeparam>
        /// <param name="doc"></param>
        /// <param name="filter"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public long Count<TDoc>(Expression<Func<TDoc, bool>> filter, FindOptions options = null)
        {
            string collectionName = typeof(TDoc).Name;
            return Count<TDoc>(collectionName, filter, options);
        }


        /// <summary>
        /// 数量
        /// </summary>
        /// <typeparam name="TDoc"></typeparam>
        /// <param name="collectionName"></param>
        /// <param name="doc"></param>
        /// <param name="filter"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public long Count<TDoc>(string collectionName, Expression<Func<TDoc, bool>> filter, FindOptions options = null)
        {
            var colleciton = GetMongoCollection<TDoc>(collectionName);
            return colleciton.Find(filter, options).CountDocuments();
        }


        /// <summary>
        /// mongodb 聚合
        /// </summary>
        /// <typeparam name="TDocument"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="doc"></param>
        /// <param name="pipeline"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public List<TResult> Aggregate<TDocument, TResult>(List<string> pipelines, AggregateOptions options = null)
        {
            string collectionName = typeof(TDocument).Name;
            return Aggregate<TDocument, TResult>(collectionName, pipelines, options);
        }


        /// <summary>
        /// mongodb 聚合
        /// </summary>
        /// <typeparam name="TDocument"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="collectionName"></param>
        /// <param name="pipeline"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public List<TResult> Aggregate<TDocument, TResult>(string collectionName, List<string> pipelines, AggregateOptions options = null)
        {
            var colleciton = GetMongoCollection<TDocument>(collectionName);
            var pipelineDef = PipelineDefinition<TDocument, TResult>.Create(pipelines);
            var retAggregate = colleciton.Aggregate<TResult>(pipelineDef, options);
            //retAggregate.MoveNext();
            var result = retAggregate.ToList();
            return result;
        }

这里只有部分核心代码,要获取全部帮助类代码请结合之前关于MongoDB的博客文章。

第二 业务代码中封装公共方法

 /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="collectionNames"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public List<T2> GetLogsToMongo<T1,T2>(string collectionNames, List<string> obj)
        {
            var list = MongoDB.MongoTool.MongoDBHelper.Aggregate<T1,T2>(collectionNames, obj);
            return list;
        }

第三 使用$group 根据时间(每小时)和ticket_type分组统计数据

 /// <summary>
        /// 获取闸机票务种类统计 当天 用于安博会
        /// </summary>
        /// <param name="collectionName">表名称</param>
        /// <param name="company_uuid">所属公司</param>
 /// <param name="length ">过去几小时数据</param>
        /// <returns></returns>
public TicketTypeData GetDeviceTicketTypeCountByDayOnAli(string collectionName, string company_uuid, int length = 0)
        {
            TicketTypeData ticketTypeData = new TicketTypeData();
           


            var start =  DateTime.Now.AddHours(-8).AddHours(-length).ToString("yyyy-MM-ddTHH:mm:ss.001");

            var end = DateTime.Now.AddHours(-8).ToString("yyyy-MM-ddTHH:mm:ss.999");

            string whereRem = "{$match:{function_name:'Ticket_CheckTicket',create_time:{$gte:ISODate('" + start + "'),$lte:ISODate('" + end + "')}}}";
            string grouupRem = "{$group:{_id:{'key':{$hour: '$create_time' },'type':'$ticket_type'},'value':{$sum:NumberInt(1)}}}";
            var list = mongoDBData.GetLogsToMongo<ReportLogs, ReportDataCountMongTicket>(collectionName + "_" + DateTime.Now.ToString("yyyyMMdd"), new List<string> { whereRem, grouupRem });

            for (int i = 0; i < list.Count; i++)
            {
                if (Convert.ToInt32(list[i]._id.key) + 8 >= 24)
                {
                    list[i]._id.key = list[i]._id.key + 8 - 24;
                }
                else
                {
                    list[i]._id.key = list[i]._id.key + 8;
                }
            }
            DateTime date = new DateTime();

            date = DateTime.Parse(DateTime.Now.AddHours(-length).ToString("yyyy-MM-dd HH:00:00"));
            List<string> dateList = new List<string>();
            List<int> IDList = new List<int>();
            List<int> ICList = new List<int>();
            List<int> QRList = new List<int>();
            List<int> FAList = new List<int>();
            for (int d = 0; d <= length; d++)
            {
                int ID = 0, IC = 0, QR = 0, FA = 0;
                dateList.Add(date.AddHours(d).Hour.ToString() + "时");
                
                ReportDataCount reportDataCount = new ReportDataCount();
                foreach (var item in list)
                {

                    if (date.AddHours(d).Hour == item._id.key)
                    {
                        switch (item._id.type)
                        {
                            case "ID":
                                ID+=item.value;
                                break;
                            case "IC":
                                IC += item.value;
                                break;
                            case "QR":
                                QR += item.value;
                                break;
                            case "FA":
                                FA += item.value;
                                break;
                        }
                       

                    }

                }
                

                IDList.Add(ID);
                ICList.Add(IC);
                QRList.Add(QR);
                FAList.Add(FA);

                
            }

            ticketTypeData.date = dateList;
            ticketTypeData.typeID = IDList;
            ticketTypeData.typeIC = ICList;
            ticketTypeData.typeQR = QRList;
            ticketTypeData.typeFA = FAList;
            return ticketTypeData;


        }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QFN-齐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值