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 {//<summa Ry>///Find the amount of UV in the date range//</summary>//<param name= "Argsatartdate" ></param> <param name= "Argenddate" ></param>///<returns></returns> public List<adid Widsid> getuvgroupbysid (DateTime argsatartdate, datetime argenddate) {var vmongoclient = new Mongocl Ient (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 BSONJAVASC Ript ("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> Find the number of UV sites within a date range//</summary>//<param name= "argsatartdate" ></param>//&L T;param name= "Argenddate" ></param>//<param name= "SID" ></param>//<RETURNS>&L T;/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 BSONJAVASC Ript ("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>//Feedback Group data by date range and SID///</summary>//<param N Ame= "Argsatartdate" ></param>//<param name= "argenddate" ></param>//<param name= " SID "></param>//<returns></returns> public list<adidwidsid> getuvgroupbyfeedback (DateTime a Rgsatartdate, 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>//Find DayEach page type name that produces Uvs in the period///</summary>//<param name= "argsatartdate" ></param>//<pa Ram Name= "Argenddate" ></param>//<param name= "Argpagetype" ></param>//<param Nam E= "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 Bsonj Avascript ("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>//Find UV//</summary>//<param name= "Argsatartdate" in the date range ></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 ar genddate, 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 {"S ID ", 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 Bsond ocument{{"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 Group, the two ways to remember first