linq - MongoDB C# - how to do the following -


how do following using mongodb c# driver .net.

say have following collection (ignore if there syntax errors):

{_id: "2312321321sdd", "ticker": "aapl", "companyname" : "apple", "viewed": "2013-05-13"} {_id: "2312321321sde", "ticker": "aapl", "companyname" : "apple", "viewed": "2013-05-12"} {_id: "2312321321sdf", "ticker": "goog", "companyname" : "google", "viewed": "2013-05-12"} {_id: "2312321321sdg", "ticker": "msft", "companyname" : "microsoft", "viewed": "2013-05-12"} {_id: "2312321321sdh", "ticker": "msft", "companyname" : "microsoft", "viewed": "2013-05-11"} 

using c# and/or linq mongodb driver, how latest 3 viewed items distinct well, there no double ups of same ticker. ideally following returned:

{_id: "2312321321sdd", "ticker": "aapl", "companyname" : "apple", "viewed": "2013-05-13"} {_id: "2312321321sdf", "ticker": "goog", "companyname" : "google", "viewed": "2013-05-12"} {_id: "2312321321sdg", "ticker": "msft", "companyname" : "microsoft", "viewed": "2013-05-12"} 

thanks in advance....

i can't either uses linq or c#, it's you're after in mongodb c# today. unfortunately, collection.asqueryable() won't far.

aggregation framework or mapreduce way go. set of distinct tickers needs modest, here's af:

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using mongodb.bson; using mongodb.driver; using mongodb.driver.linq;  namespace consoleapplication1 {     class program     {         static void main(string[] args)         {             mongocollection<tick> coll =                 new mongoclient("mongodb://11.11.11.11/test").getserver()                                                                 .getdatabase("test")                                                                 .getcollection<tick>("tix");              var group = new bsondocument                 {                     {                         "$group",                         new bsondocument                             {                                 {                                     "_id",  "$companyname"                                 },                                 {                                     "lastviewed", new bsondocument                                         {                                             {                                                 "$max", "$viewed"                                             }                                         }                                 }                             }                     }                 };              foreach (                 var t in                     coll.aggregate(group,                                    new bsondocument("$sort", new bsondocument("lastviewed", -1)),                                    new bsondocument("$limit", 3))                         .resultdocuments)             {                 console.writeline("{0} last viewed {1}", t["_id"], t["lastviewed"]);             }     }      public class tick     {         public string id { get; set; }         public string ticker { get; set; }         public string companyname { get; set; }         public string viewed { get; set; }     } } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -