Предполагая, что у меня есть следующая коллекция:
Код: Выделить всё
{ "_id" : "1", "Contents":{"item11":{}, "item12":{}, "summary":"1" } },
{ "_id" : "2", "Contents":{"item21":{}, "summary":"2" } },
{ "_id" : "3", "Contents":{"item31":{}, "item32":{}, "item33":{}, "summary":"" } }
Код: Выделить всё
{ {Id:"1", "ContentCategories":["item11", "item12", "summary"]},
{Id:"2", "ContentCategories":["item21", "summary"]},
{"Id":"3", "ContentCategories":["item31", "item32", "item33", "summary"] }
Код: Выделить всё
class Program
{
public sealed class DataRM
{
[BsonId]
public string _id { get; set; }
public Dictionary Contents { get; set; } = new();
}
public class Dto
{
public string Id { get; set; }
public List ContentCategories { get; set; }
public string Summary { get; set; }
}
public static void Main(string[] args)
{
var client = new MongoClient($"mongodb://root:example@server.address:27017");
var database = client.GetDatabase("Test");
var collection = database.GetCollection("temp");
var filter = Builders.Filter.Empty;
var records = collection.Find(filter).ToList();
var result =
records.Select(
x => new Dto
{
Id = x._id,
ContentCategories = x.Contents.Keys.ToList(),
Summary = x.Contents["summary"] as string
});
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... dictionary
Мобильная версия