mongodb分组,的两种方式,先记一下

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using NationalUnion.AdGallery.Model;
using NationalUnion.Common.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NationalUnion.AdGallery.DALMongo
{
    public class AdUVDAL
    {
        /// 
        /// 查找日期范围内的UV量
        /// 
        /// 
        /// 
        /// 
        public List GetUVGroupBySID(DateTime argSatartDate, DateTime argEndDate)
        {
            var vMongoClient = new MongoClient(ConstUtinity.MongoDBCon);
            var vServer = vMongoClient.GetServer();
            var vDataBase = vServer.GetDatabase(ConstUtinity.DATABASE);
            var vCollection = vDataBase.GetCollection(ConstUtinity.ADUVCOL);

            var vQueryDateStart = Query.GTE(x => x.CrateDate, argSatartDate);
            var vQueryDateEnd = Query.LTE(x => x.CrateDate, argEndDate);

            IMongoQuery vQuery = Query.And(vQueryDateStart, vQueryDateEnd);
            Dictionary InitailDIC = new Dictionary();
            InitailDIC["num"] = 0;
            var vQuryble = vCollection.Group(vQuery, "SID",
                 BsonDocument.Create(InitailDIC),
            new BsonJavaScript("function(doc,prev){prev.num++;}"),
             new BsonJavaScript("function(doc,out){doc.Count=doc.num;delete doc.num }"));

            List vAdIDWIDList = new List();
            foreach (var item in vQuryble)
            {
                vAdIDWIDList.Add(new AdIDWIDSID()
                {
                    SID = item["SID"].AsInt(),
                    Count = item["Count"].AsInt()
                });
            }
            return vAdIDWIDList;
        }

        /// 
        /// 查找日期范围内的各站点UV量
        /// 
        /// 
        /// 
        /// 
        /// 
        public List GetSiteUV(DateTime argSatartDate, DateTime argEndDate,int SID)
        {
            var vMongoClient = new MongoClient(ConstUtinity.MongoDBCon);
            var vServer = vMongoClient.GetServer();
            var vDataBase = vServer.GetDatabase(ConstUtinity.DATABASE);
            var vCollection = vDataBase.GetCollection(ConstUtinity.ADUVCOL);

            var vSIDQuery = Query.EQ(x => x.SID, SID);
            var vQueryDateStart = Query.GTE(x => x.CrateDate, argSatartDate);
            var vQueryDateEnd = Query.LTE(x => x.CrateDate, argEndDate);

            IMongoQuery vQuery = Query.And(vQueryDateStart, vQueryDateEnd, vSIDQuery);
            Dictionary InitailDIC = new Dictionary();
            InitailDIC["num"] = 0;
            var vQuryble = vCollection.Group(vQuery, "WID",
                 BsonDocument.Create(InitailDIC),
            new BsonJavaScript("function(doc,prev){prev.num++;}"),
             new BsonJavaScript("function(doc,out){doc.Count=doc.num;delete doc.num }"));

            List vAdIDWIDList = new List();
            foreach (var item in vQuryble)
            {
                vAdIDWIDList.Add(new AdIDWIDSID()
                {
                    WID = item["WID"].AsInt(),
                    Count = item["Count"].AsInt()
                });
            }
            return vAdIDWIDList;
        }
        
        /// 
        /// 根据日期范围和SID对数据FeedBack分组
        /// 
        /// 
        /// 
        /// 
        /// 
        public List GetUVGroupByFeedBack(DateTime argSatartDate, DateTime argEndDate, int SID)
        {
            var vMongoClient = new MongoClient(ConstUtinity.MongoDBCon);
            var vServer = vMongoClient.GetServer();
            var vDataBase = vServer.GetDatabase(ConstUtinity.DATABASE);
            var vCollection = vDataBase.GetCollection(ConstUtinity.ADUVCOL);

            var vSIDQuery = Query.EQ(x => x.SID, SID);
            var vQueryDateStart = Query.GTE(x => x.CrateDate, argSatartDate);
            var vQueryDateEnd = Query.LTE(x => x.CrateDate, argEndDate);

            IMongoQuery vQuery = Query.And(vQueryDateStart, vQueryDateEnd, vSIDQuery);
            string mapfunction = @"function Map() {
                                        if(this.FeedBack.length>0)
                                        {
	                                        var FeedBackID=this.FeedBack.split('|');
	                                        if(FeedBackID.length>0)
	                                        {
		                                        emit(FeedBackID[0],{count:1});
	                                        }
                                        }
                                  }";
            string reducefunction = @"function Reduce(key, values) {
	                                var count=0;
	                                for(var i=0;i
        /// 查找日期范围内产生UV的各个页面类型名
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public List GetGomePageUV(DateTime argSatartDate, DateTime argEndDate, int argPageType, int SID, string argUrl)
        {
            var vMongoClient = new MongoClient(ConstUtinity.MongoDBCon);
            var vServer = vMongoClient.GetServer();
            var vDataBase = vServer.GetDatabase(ConstUtinity.DATABASE);
            var vCollection = vDataBase.GetCollection(ConstUtinity.ADUVCOL);

            var vQueryDateStart = Query.GTE(x => x.CrateDate, argSatartDate);
            var vQueryDateEnd = Query.LTE(x => x.CrateDate, argEndDate);

            IMongoQuery vQuery = Query.And(vQueryDateStart, vQueryDateEnd);
            if (SID > 0)
            {
                var vQuerySID = Query.EQ(x => x.SID, SID);
                vQuery = Query.And(vQuery, vQuerySID);
            }
            if (!string.IsNullOrWhiteSpace(argUrl))
            {
                var vQueryRawUrl = Query.EQ(x => x.RootUrl, argUrl);
                vQuery = Query.And(vQuery, vQueryRawUrl);
            }

            if (argPageType > -1)
            {
                var vQueryPageType = Query.EQ(x => x.PageType, argPageType);
                vQuery = Query.And(vQuery, vQueryPageType);
            }

            Dictionary InitailDIC = new Dictionary();
            InitailDIC["num"] = 0;
            var vQuryble = vCollection.Group(vQuery, "PageType",
                 BsonDocument.Create(InitailDIC),
            new BsonJavaScript("function(doc,prev){prev.num++;}"),
             new BsonJavaScript("function(doc,out){doc.Count=doc.num;delete doc.num }"));

            List vAdIDWIDList = new List();
            foreach (var item in vQuryble)
            {
                vAdIDWIDList.Add(new AdIDWIDSID()
                {
                    PageType = item["PageType"].AsInt(),
                    Count = item["Count"].AsInt()
                });
            }
            return vAdIDWIDList;
        }


        /// 
        /// 查找日期范围内单品的UV
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public List GetProductUV(DateTime argSatartDate, DateTime argEndDate, int argPageType, int argSID, string argUrl)
        {
            var vMongoClient = new MongoClient(ConstUtinity.MongoDBCon);
            var vServer = vMongoClient.GetServer();
            var vDataBase = vServer.GetDatabase(ConstUtinity.DATABASE);
            var vCollection = vDataBase.GetCollection(ConstUtinity.ADUVCOL);

            #region march
            BsonDocument vMarchBson = new BsonDocument { 
                {"CrateDate", new BsonDocument { 
                    { "$gte", argSatartDate },
                    { "$lte", argEndDate } } 
                }
            };
            if (argPageType > -1)
            {
                vMarchBson.AddRange(new BsonDocument { 
                    {"PageType",argPageType}
                });
            }
            if (argSID > 0)
            {
                vMarchBson.AddRange(new BsonDocument { 
                    {"SID",argSID}
                });
            }

            if (!string.IsNullOrWhiteSpace(argUrl))
            {
                vMarchBson.AddRange(new BsonDocument { 
                    {"RootUrl",argUrl}
                });
            }

            BsonDocument vMatch = new BsonDocument();
            vMatch.AddRange(new BsonDocument { { "$match", vMarchBson } });
            #endregion
            BsonDocument vBsonLimit = new BsonDocument{
                {"$limit",50}
             };

            BsonDocument vBsonGroup = new BsonDocument{
                {"$group",new BsonDocument{
                    {"_id",new BsonDocument{{"RootUrl","$RootUrl"}}},
                    {"Count",new BsonDocument{{"$sum",1}}}
                    }
                }
             };

            BsonDocument vBsonSort = new BsonDocument { 
               {"$sort",new BsonDocument{{"Count",-1}}}
            };


             AggregateArgs vAggregateArgs = new AggregateArgs();
             List vBsonDocumentList = new List();
             vBsonDocumentList.Add(vMatch);
             vBsonDocumentList.Add(vBsonLimit);
             vBsonDocumentList.Add(vBsonGroup);
             vBsonDocumentList.Add(vBsonSort);

             vAggregateArgs.Pipeline = vBsonDocumentList;
             var vResult = vCollection.Aggregate(vAggregateArgs);


             List vResultModelList = new List();
             foreach (var item in vResult)
             {
                 vResultModelList.Add(new ResultModel()
                 {
                     RootUrl = item["_id"]["RootUrl"].AsString(),
                     Count = item["Count"].AsInt()
                 });
             }

             return vResultModelList;
        } 
    }
}

 

转载于:https://www.cnblogs.com/xuxu-dragon/p/4088852.html

你可能感兴趣的:(mongodb分组,的两种方式,先记一下)