mongodb分組,的兩種方式,先記一下

來源:互聯網
上載者:User

標籤:blog   io   ar   java   sp   for   資料   div   on   

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    {        /// <summary>        /// 尋找日期範圍內的UV量        /// </summary>        /// <param name="argSatartDate"></param>        /// <param name="argEndDate"></param>        /// <returns></returns>        public List<AdIDWIDSID> 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<AdUV>(ConstUtinity.ADUVCOL);            var vQueryDateStart = Query<AdUV>.GTE(x => x.CrateDate, argSatartDate);            var vQueryDateEnd = Query<AdUV>.LTE(x => x.CrateDate, argEndDate);            IMongoQuery vQuery = Query.And(vQueryDateStart, vQueryDateEnd);            Dictionary<string, int> InitailDIC = new Dictionary<string, int>();            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<AdIDWIDSID> vAdIDWIDList = new List<Model.AdIDWIDSID>();            foreach (var item in vQuryble)            {                vAdIDWIDList.Add(new AdIDWIDSID()                {                    SID = item["SID"].AsInt(),                    Count = item["Count"].AsInt()                });            }            return vAdIDWIDList;        }        /// <summary>        /// 尋找日期範圍內的各網站UV量        /// </summary>        /// <param name="argSatartDate"></param>        /// <param name="argEndDate"></param>        /// <param name="SID"></param>        /// <returns></returns>        public List<AdIDWIDSID> 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<AdUV>(ConstUtinity.ADUVCOL);            var vSIDQuery = Query<AdUV>.EQ(x => x.SID, SID);            var vQueryDateStart = Query<AdUV>.GTE(x => x.CrateDate, argSatartDate);            var vQueryDateEnd = Query<AdUV>.LTE(x => x.CrateDate, argEndDate);            IMongoQuery vQuery = Query.And(vQueryDateStart, vQueryDateEnd, vSIDQuery);            Dictionary<string, int> InitailDIC = new Dictionary<string, int>();            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<AdIDWIDSID> vAdIDWIDList = new List<Model.AdIDWIDSID>();            foreach (var item in vQuryble)            {                vAdIDWIDList.Add(new AdIDWIDSID()                {                    WID = item["WID"].AsInt(),                    Count = item["Count"].AsInt()                });            }            return vAdIDWIDList;        }                /// <summary>        /// 根據日期範圍和SID對資料FeedBack分組        /// </summary>        /// <param name="argSatartDate"></param>        /// <param name="argEndDate"></param>        /// <param name="SID"></param>        /// <returns></returns>        public List<AdIDWIDSID> 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<AdUV>(ConstUtinity.ADUVCOL);            var vSIDQuery = Query<AdUV>.EQ(x => x.SID, SID);            var vQueryDateStart = Query<AdUV>.GTE(x => x.CrateDate, argSatartDate);            var vQueryDateEnd = Query<AdUV>.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<values.length;i++){                                count+=[values].count;                                }                                return {FeedBack:key,UV:count};                                }";                        BsonJavaScript vMaP = new BsonJavaScript(mapfunction);            BsonJavaScript vreduce = new BsonJavaScript(reducefunction);            MapReduceResult vResult = vCollection.MapReduce(vQuery, vMaP, vreduce);            return null;        }        /// <summary>        /// 尋找日期範圍內產生UV的各個頁面類型名        /// </summary>        /// <param name="argSatartDate"></param>        /// <param name="argEndDate"></param>        /// <param name="argPageType"></param>        /// <param name="SID"></param>        /// <param name="argUrl"></param>        /// <returns></returns>        public List<AdIDWIDSID> 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<AdUV>(ConstUtinity.ADUVCOL);            var vQueryDateStart = Query<AdUV>.GTE(x => x.CrateDate, argSatartDate);            var vQueryDateEnd = Query<AdUV>.LTE(x => x.CrateDate, argEndDate);            IMongoQuery vQuery = Query.And(vQueryDateStart, vQueryDateEnd);            if (SID > 0)            {                var vQuerySID = Query<AdUV>.EQ(x => x.SID, SID);                vQuery = Query.And(vQuery, vQuerySID);            }            if (!string.IsNullOrWhiteSpace(argUrl))            {                var vQueryRawUrl = Query<AdUV>.EQ(x => x.RootUrl, argUrl);                vQuery = Query.And(vQuery, vQueryRawUrl);            }            if (argPageType > -1)            {                var vQueryPageType = Query<AdUV>.EQ(x => x.PageType, argPageType);                vQuery = Query.And(vQuery, vQueryPageType);            }            Dictionary<string, int> InitailDIC = new Dictionary<string, int>();            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<AdIDWIDSID> vAdIDWIDList = new List<Model.AdIDWIDSID>();            foreach (var item in vQuryble)            {                vAdIDWIDList.Add(new AdIDWIDSID()                {                    PageType = item["PageType"].AsInt(),                    Count = item["Count"].AsInt()                });            }            return vAdIDWIDList;        }        /// <summary>        /// 尋找日期範圍內單品的UV        /// </summary>        /// <param name="argSatartDate"></param>        /// <param name="argEndDate"></param>        /// <param name="argPageType"></param>        /// <param name="SID"></param>        /// <param name="argUrl"></param>        /// <returns></returns>        public List<ResultModel> 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<AdUV>(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<BsonDocument> vBsonDocumentList = new List<BsonDocument>();             vBsonDocumentList.Add(vMatch);             vBsonDocumentList.Add(vBsonLimit);             vBsonDocumentList.Add(vBsonGroup);             vBsonDocumentList.Add(vBsonSort);             vAggregateArgs.Pipeline = vBsonDocumentList;             var vResult = vCollection.Aggregate(vAggregateArgs);             List<ResultModel> vResultModelList = new List<ResultModel>();             foreach (var item in vResult)             {                 vResultModelList.Add(new ResultModel()                 {                     RootUrl = item["_id"]["RootUrl"].AsString(),                     Count = item["Count"].AsInt()                 });             }             return vResultModelList;        }     }}

 

mongodb分組,的兩種方式,先記一下

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.