1, using aggregation to get the latest real-time data (each station has more than one data, take the date of the latest data. That is, each station takes the latest value)
varGroup =Newbsondocument {{"_id",Newbsondocument {{"StationID","$stationID"}, {"StationName","$stationName" } } }, //{"StationID", new bsondocument{{"StationID", "$stationID"}}},{"StationID",NewBsondocument ("$last","$stationID")}, { "Timedata",NewBsondocument ("$max","$TimeData")}, {"Evalue",NewBsondocument ("$last","$eValue")} };
var collection = modatabase.getcollection<bsondocument> (tablename);
varList =collection. Aggregate (). Group (group). Tolistasync (). Result; DoubleValue =0.0; foreach(Bsondocument Bsondocinchlist) { DoubleEvalue =Double. TryParse (Bsondoc. GetElement ("Evalue"). Value.tostring (), outValue)? Value:0; stringStationid = Bsondoc. GetElement ("StationID"). Value.tostring ();
2, normal according to the order to take the latest data
If a station is a table name
/// <summary> ///get the time for each data in the database and put it on the horizontal axis of the chart/// </summary> /// <param name= "statioid" >Station ID</param> /// <param name= "Pullutionid" >Indicator Type</param> /// <returns></returns> Public Staticlist<string> Getchartdata (stringStatioid,stringPullutionid,stringStartTime,stringEndTime) {List<string> XData =Newlist<string>(); if(ConnectionString = =NULL) Oncreatedb (); Modatabase= Moclient.getdatabase ("Ssmonitor"); Bsondocument Bsondoc=Newbsondocument (); Bsondoc.add ("Timedata",NewBsondocument () {{"$gte", Datetimetoint (StartTime)}, {"$lte", Datetimetoint (EndTime)}}); varCollection = Modatabase.getcollection<bsondocument> (string. Format ("{0}_{1}_day", Statioid, Pullutionid)); varsort = builders<bsondocument>. Sort.ascending ("Timedata"); varList =collection. Find (Bsondoc). Sort (sort). ToList (); foreach(Bsondocument Bsondocinchlist) { stringTime = Bsondoc. GetElement ("Monitortime"). Value.tostring (); Xdata.add (time); } returnXData; }
C # Common code for MONGDB fetching data