c# - Sort MongoDB result on aggregation of list element field -


i started play around mongodb on c#. use restaurant sample data (https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json).

what trying figure out is, how can sort restaurants total of rating score. can provide sample on how using aggregatefluent api? got lost that.

thanks!

i create dto classes collection:

public class restaurant {     public objectid _id { get; set; }     public address address { get; set; }     public string borough { get; set; }     public string cuisine { get; set; }     public grades[] grades {get;set;}     public string name { get; set; }     public string restaurant_id {get;set;} }  public class grades {     public datetime date {get;set;}     public string grade {get;set;}     public int? score {get;set;} }  public class address {     public string building { get; set; }     public double[] coord { get; set; }     public string street { get; set; }     public string zipcode { get; set;} } 

and if create collection :

var collection = db.getcollection<restaurant>("restaurants"); 

you order result way:

collection     .aggregate()     .project(r => new {restarant = r.name, totalscore= r.grades.sum(g=>g.score)})     .sortbydescending(x=>x.totalscore)     .tolist() 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -