【发布时间】:2016-03-15 17:22:27
【问题描述】:
我有一个包含半复杂记录的 MongoDB 数据库,随着集合大小的增加,我的报告查询遇到了困难。我想制作一些针对快速搜索和聚合而优化的报告视图。这是一个示例格式:
var record = {
fieldOne:"",
fieldTwo:"",
fieldThree:"", //There is approx 30 fields at this level
ArrayOne:[
{subItem1:""},
{subItem2:""} // There are usually about 10-15 items in this array
],
ArrayTwo:[
{subItem1:""}, //ArrayTwo items reference ArrayOne item ids for ref
{subItem2:""} // There are usually about 20-30 items in this array
],
ArrayThree:[
{subItem1:""},// ArrayThree items reference both ArrayOne and ArrayTwo items for ref
{subItem2:""},// There are usually about 200-300 items in this array
{subArray:[
{subItem1:""},
{subItem2:""} // There are usually about 5 items in this array
]}
]
};
我曾经拥有这些数据,其中ArrayTwo 在 ArrayOne 项目内,ArrayThree 在 ArrayTwo 项目内,因此隐含了引用父项,但报告成为具有多个嵌套级别数组的噩梦。
我在每个级别都有一个名为“fieldName”的字段,这是我们在数组中定位对象的一种方式。
我经常需要从查询中的数千条记录中的 3 个数组中的任何一个中聚合值。
我看到了两种方法。
A)。展平并垂直移动,在数据库中为ArrayThree 中的每个项目创建一个较小的记录,基本上每个复杂记录添加 200 条记录。我试过了,在 5 天内我已经有 20 万条记录进入新数据。这样做的好处是我有字段名称,我可以在上面添加索引。
B)。水平展平,使每个阵列都在单个集合记录中变平。我将使用位于每个数组对象中的 FieldName 作为key。这将创建一条包含 200-300 个字段的记录。这会减少集合中的记录,但字段将是动态的,因此无法添加索引(据我所知)。
目前,我有大约 30 万条现有记录可以用来构建此视图。如果我是垂直的,那将在数据库中放置 6000 万条简单记录,如果我是水平的,它将是 300K 记录,每个记录有 200 个展平,没有索引能力。
解决这个问题的正确方法是什么?
【问题讨论】:
标签: mongodb data-structures aggregation-framework reporting database