Using Spring data MongoDB v1.8
Demand 1,
The data structure is as follows. Description: Change the collection record is the company each system's access situation (LocalPath represents the system, RequestTime represents the request time, the string type,)
Requirements: Select a day and then group by system and hour to count the access of each system per hour of the day.
Business code:
Criteria criteria = new criteria (); Criteria.andoperator (Criteria.where ("CreateTime"). LT ( Dateutil.addday (Sdf.parse (Sdf.format (date)), 1), criteria.where ("Createtime"). GTE (Sdf.parse ( Sdf.format (date)));// Matching query Matchoperation matchoperation = aggregation.match (criteria);// localPath// returns the parameter Projectionoperation return1 = aggregation.project ("LocalPath"). Andexpression ("substr (requesttime,11,2)"). As ("Dimension");// conditionally grouped groupoperation go2 = Aggregation.group ("Dimension", "LocalPath"). Count (). As ("Times");// set sort sortoperation Sortoperation = aggregation.sort (sort.direction.asc, "LocalPath", "Dimension");// Build parameter Aggregation aggregation = aggregation.newaggregation (matchoperation, return1, go2 , sortoperation);// Group Aggregation query aggregationresults<syscountresultvo> aggregate = Mongotemplate.aggregate (Aggregation, getCollectionName (date), syscountresultvo.class);// get results List<syscountresultvo> resultvos = aggregate.getmappedresults ();
Syscountresultvo class
public class syscountresultvo { /** Times **/ private int times; /** Service **/ private string localpath; /** Dimension **/ private string dimension; public int gettimes () {return times; } public void settimes (int times) {this.times = times; } public string getlocalpath () {return localPath; } public void setlocalpath (String localPath) {this.localPath = localPath; } public String getdimension () {return dimension; } public void setdimension (string d imension) {this.dimension = dimension; } @ Override public string tostring () {return "syscountresultvo [ Times= " + times + ", localpath= " + localPath + ", dimension= " + dimension + "] "; }}
Demand Two,
The data structure is as follows. Description: The collection stores the posting information (the type represented by Isview, Deliverytime represents the delivery time)
Demand:
Select a time period that requires grouping statistics by day and delivery type.
Difficulties:
MongoDB storage time is UTC time, which is 8 hours less than local time, for example, local time is: 2018.7.18 00:00:00 day, MongoDB is stored in 2018.7.17 t16:00:00z things.
By day, if a character is stored in the collection, you can use the method in requirement 1 to process it using the SUBSTR function. But what if there are no strings stored as required in 2? Here's my idea.
Service
Criteria criteria = new criteria ();/** here set match criteria **/// match query matchoperation Matchoperation = aggregation.match (criteria);// returns a parameter to process the date projectionoperation return1 = aggregation.project ("Isview"). Andexpression ("Year (Deliverytime)"). As ("year"). Andexpression ("Month (Deliverytime). As ("month"). Andexpression ("DayOfMonth (Deliverytime)"). As ("Day"). Andexpression ("Hour ( Deliverytime). As ("Hour");// grouped by conditions Groupoperation go2 = aggregation.group ("Isview", " Year ", " month ", " Day ", " Hour "). Count (). As (" Times ");// set sort sortoperation sortoperation = aggregation.sort (sort.direction.asc, "Times");// Build Parameters aggregation aggregation = aggregation.newaggregation (matchoperation, return1, go2, sortoperation);// Group Aggregation Query Aggregationresults<syscountresultvo> aggregate = mongotemplate.aggregate ( aggregation, "Resumedeliveryrecordvo", Syscountresultvo.class);// Get results list<syscountresultvo> resultvos = Aggregate.getmappedresults ();
Syscountresultvo class
public class syscountresultvo { /** Times **/ private int times; /** Service **/ private string isview; /** year **/ private integer year; /** Month **/ private integer month; /** Day **/ private integer day; /** hours **/ private integer hour; /** Time **/ private string time; private String isViewStr; public int Gettimes () {return times; } public void Settimes (int times) {this.times = times; } Public string getisvieW () {return isview; } public void setisview ( String isview) {this.isView = isView; } Public integer getyear () {return year; } Public void setyear (Integer year) {this.year = year; } public integer getmonth () {return month; } public void setmonth (Integer month) {this.month = month; } public integer getday () {return day; } public void setday (Integer day) {this.day = day; } // here is the focus, if >=16, is considered the next day public stRing gettime () {try { if (hour >= 16) {return dateutil.date2datestr (Dateutil.addday (Dateutil.datestr2date (this.getyear) + "-" + This.getmonth () + "-" + this.getday (), dateutil.pattern_dtshortline), 1), Dateutil.pattern_dtshortline); } return this.getyear () + "-" + this.getmonth () + "-" + this.getday ();} catch (exception e) { e.printstacktrace ();} Return null; } public integer gethour () { return hour; } public void sethour (Integer hour) { This.hour = hour; } &nBsp;public void settime (String time) {this.time = time; } public string getisviewstr () {return integer.valueof (IsView) == 1 ? "Not viewed" : integer.valueof (Isview) == 2 ? "to communicate" : Integer.valueof (Isview) == 4 ? "viewed" : integer.valueof (Isview) == 6 ? "Inappropriate" : "other"; } public void Setisviewstr (STRING&NBSP;ISVIEWSTR) {this.isViewStr = isViewStr; } @Override public string tostring () {return "Time: " + gettime () + "; Times: " + gettimes () + "; Isview: " + Getisview (); }}
It may not be clear, but it can be used as a reference. My qq:1208576787, if have any question, can add QQ discussion under.
MongoDB by Time Grouping statistics