C # Common code for MONGDB fetching data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.