In the near future I want to migrate from SQL Server to MongoDB. For now I'm just playing around with all the possibilities and testing out the best schema design. I stumbled on a question. Let me summarize the issue.
I have 30 manufacturers that each contains a list of size ranges (like S -> XXL). Each size range has list of sizes (like S, M, L, XL, XXL). So this is the class I've created:
public class Manufacturer : BaseEntity
{
#region Ctor
public Manufacturer()
{
SizeRanges = new List<SizeRange>();
}
#endregion
#region Properties
public string Name { get; set; }
public string Description { get; set; }
public string MetaTitle { get; set; }
public string MetaDescription { get; set; }
public double PurchaseDiscountPercentage { get; set; }
public bool LimitedToStores { get; set; }
public int DisplayOrder { get; set; }
public State State { get; set; }
public bool SyncToShop { get; set; }
public bool Deleted { get; set; }
public bool ManufactuerTierPriceHasChanged { get; set; }
public bool ManufactuerSizesHasChanged { get; set; }
public bool PurchaseDiscountPercentageChanged { get; set; }
public DateTime? DateChanged { get; set; }
public DateTime? DateCreated { get; set; }
#endregion
#region Mapping
public Picture Picture { get; set; }
public List<SizeRange> SizeRanges { get; set; }
#endregion
#region Classes
public class SizeRange
{
// Ctor
public SizeRange()
{
Sizes = new List<Size>();
}
// Properties
public string Name { get; set; }
public int DisplayOrder { get; set; }
public bool Deleted { get; set; }
public DateTime? DateChanged { get; set; }
public DateTime? DateCreated { get; set; }
// Mapping
public List<Size> Sizes { get; set; }
}
public class Size
{
// Properties
public string SizeName { get; set; }
public string LookupSize { get; set; }
public int DisplayOrder { get; set; }
public bool Deleted { get; set; }
public DateTime? DateChanged { get; set; }
public DateTime? DateCreated { get; set; }
}
#endregion
}
Now here is my question. What is the best way to get all the size ranges? I wrote this piece of code using mongoDB 3.2. I'm using 2 steps here but think there is a better way to get all the size ranges from all the manufacturers. If there is a better way, just let me know.
var sizeRanges = await erpContext
.Manufacturers
.Find(FilterDefinition<MongoManufacturer>.Empty)
.Project(x=> x.SizeRanges)
.ToListAsync(); // results in a list containing 30 collections of size ranges
var sizeRangesList = sizeRanges
.SelectMany(x => x)
.ToList(); // get a list with only the size ranges.