首页 > 代码库 > mongodb分组,的两种方式,先记一下
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 { /// <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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。