对列表元素字段的聚合排序 MongoDB 结果
Sort MongoDB result on aggregation of list element field
我刚开始在 C# 上使用 MongoDB。我使用餐厅示例数据 (https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json)。
我想弄清楚的是,如何根据餐厅的总评分对餐厅进行排序。有人可以提供有关如何使用 AggregateFluent API 执行此操作的示例吗?我迷路了。
谢谢!
我会为你的 collection 创建 DTO 类:
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;}
}
如果您将 collection 创建为 :
var collection = db.GetCollection<Restaurant>("restaurants");
您可以这样排序结果:
collection
.Aggregate()
.Project(r => new {Restarant = r.name, TotalScore= r.grades.Sum(g=>g.score)})
.SortByDescending(x=>x.TotalScore)
.ToList()
我刚开始在 C# 上使用 MongoDB。我使用餐厅示例数据 (https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json)。
我想弄清楚的是,如何根据餐厅的总评分对餐厅进行排序。有人可以提供有关如何使用 AggregateFluent API 执行此操作的示例吗?我迷路了。
谢谢!
我会为你的 collection 创建 DTO 类:
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;}
}
如果您将 collection 创建为 :
var collection = db.GetCollection<Restaurant>("restaurants");
您可以这样排序结果:
collection
.Aggregate()
.Project(r => new {Restarant = r.name, TotalScore= r.grades.Sum(g=>g.score)})
.SortByDescending(x=>x.TotalScore)
.ToList()