【发布时间】:2021-12-31 22:09:24
【问题描述】:
我的 .Net / C# 应用程序在 Mongodb 中存储了一些信息。 Mongodb文档在C#中对应的结构如下:
public class GlobalInfo
{
public ObjectId Id { get; set; }
public string Country { get; set; }
public string City { get; set; }
public int Population { get; set; }
}
我想从 Mongodb 文档中提取包含所有国家/地区的 List<CountryInfo>,或者最终提取为 Dictionary<string, List<CityInfo>>(国家名称将是字典键),基于以下类:
public class CountryInfo
{
public string Name { get; set; }
public List<CityInfo> Cities { get; set; }
}
public class CityInfo
{
public string Name { get; set; }
public int Population { get; set; }
}
我找到了一些关于类似功能的 c# 示例,这些示例是在 Mongodb C# 驱动程序的 Aggregate()、Match()、Group() 方法的帮助下实现的。我不确定如何在我的示例中使用它们。
基本上,我需要将带有 Mongodb db 文档的平面列表转换为带有嵌套列表或字典的分层模型。有人可以帮忙提供一些样品吗?
【问题讨论】:
标签: c# mongodb aggregate grouping fluent