MongoDB'放松'嵌套对象

时间:2016-09-23 10:28:23

标签: mongodb

我有一个集合,其中每个文档看起来像这样:

{
_id: 'dev_id:datetime_hour',
data: {
    0: { 
        0: {
            voltage_a: float, 
            voltage_b: float, 
            voltage_c: float, 
            current_a: float, 
            current_b: float, 
            current_c: float, 
            current_n: float, 
            active_power_a: float, 
            active_power_b: float, 
            active_power_c: float, 
            total_active_power: float   
        },
        1: {
            voltage_a: float, 
            voltage_b: float, 
            voltage_c: float, 
            current_a: float, 
            current_b: float, 
            current_c: float, 
            current_n: float, 
            active_power_a: float, 
            active_power_b: float, 
            active_power_c: float, 
            total_active_power: float   
        },
        2: {
            voltage_a: float, 
            voltage_b: float, 
            voltage_c: float, 
            current_a: float, 
            current_b: float, 
            current_c: float, 
            current_n: float, 
            active_power_a: float, 
            active_power_b: float, 
            active_power_c: float, 
            total_active_power: float   
        },
        59: {
            voltage_a: float, 
            voltage_b: float, 
            voltage_c: float, 
            current_a: float, 
            current_b: float, 
            current_c: float, 
            current_n: float, 
            active_power_a: float, 
            active_power_b: float, 
            active_power_c: float, 
            total_active_power: float   
        } 
    },
    1: { 
        0: {
            voltage_a: float, 
            voltage_b: float, 
            voltage_c: float, 
            current_a: float, 
            current_b: float, 
            current_c: float, 
            current_n: float, 
            active_power_a: float, 
            active_power_b: float, 
            active_power_c: float, 
            total_active_power: float   
        },
        1: {
            voltage_a: float, 
            voltage_b: float, 
            voltage_c: float, 
            current_a: float, 
            current_b: float, 
            current_c: float, 
            current_n: float, 
            active_power_a: float, 
            active_power_b: float, 
            active_power_c: float, 
            total_active_power: float   
        },
        2: {
            voltage_a: float, 
            voltage_b: float, 
            voltage_c: float, 
            current_a: float, 
            current_b: float, 
            current_c: float, 
            current_n: float, 
            active_power_a: float, 
            active_power_b: float, 
            active_power_c: float, 
            total_active_power: float   
        },
        59: {
            voltage_a: float, 
            voltage_b: float, 
            voltage_c: float, 
            current_a: float, 
            current_b: float, 
            current_c: float, 
            current_n: float, 
            active_power_a: float, 
            active_power_b: float, 
            active_power_c: float, 
            total_active_power: float   
        } 
    }
}

我在这里简化了它,但基本思路是:传感器数据每秒存储一次,但按小时捆绑在一起。 '数据'字段按分钟索引,每分钟索引除以秒。因此,整个小时的数据将在嵌套数据字段中产生3600个条目。例如,要获取第一分钟和第三秒的传感器数据,我可以直接访问该对象:data.1.3

此类架构为recommended for storing time series data by MongoDB

我的聚合管道的第1阶段如下所示:

db.raw_electric.aggregate(
  [
    // Stage 1
    {
      $match: {
        _id: { $regex: /^r10a:/ },
        datehour: {$gte: ISODate("2016-09-21T17:00:00"), $lte: ISODate("2016-09-21T19:00:00")}
      }
    }

  ]

);

是否可以放松'文档 - 类似于你如何解开一个数组,以便我可以暴露一个对象的每个嵌套层?

2 个答案:

答案 0 :(得分:2)

您应该按照以下方式创建架构:

{
    _id: 'dev_id:datetime_hour',
    data: [{
        name: '0',
        info: [{}]
    }]
}

ieyour数据应该是一个对象数组。从该数组中,您可以使用其索引获取任何对象。

{
    _id: 'dev_id:datetime_hour',
    data: [{
            name: '0',
            info: [{
                voltage_a: float,
                voltage_b: float,
                voltage_c: float,
                current_a: float,
                current_b: float,
                current_c: float,
                current_n: float,
                active_power_a: float,
                active_power_b: float,
                active_power_c: float,
                total_active_power: float
            }, {
                voltage_a: float,
                voltage_b: float,
                voltage_c: float,
                current_a: float,
                current_b: float,
                current_c: float,
                current_n: float,
                active_power_a: float,
                active_power_b: float,
                active_power_c: float,
                total_active_power: float
            }, {
                voltage_a: float,
                voltage_b: float,
                voltage_c: float,
                current_a: float,
                current_b: float,
                current_c: float,
                current_n: float,
                active_power_a: float,
                active_power_b: float,
                active_power_c: float,
                total_active_power: float
            }, {
                voltage_a: float,
                voltage_b: float,
                voltage_c: float,
                current_a: float,
                current_b: float,
                current_c: float,
                current_n: float,
                active_power_a: float,
                active_power_b: float,
                active_power_c: float,
                total_active_power: float
            }],
            {
                name: '1',
                info: [{
                        voltage_a: float,
                        voltage_b: float,
                        voltage_c: float,
                        current_a: float,
                        current_b: float,
                        current_c: float,
                        current_n: float,
                        active_power_a: float,
                        active_power_b: float,
                        active_power_c: float,
                        total_active_power: float
                    }, {
                        voltage_a: float,
                        voltage_b: float,
                        voltage_c: float,
                        current_a: float,
                        current_b: float,
                        current_c: float,
                        current_n: float,
                        active_power_a: float,
                        active_power_b: float,
                        active_power_c: float,
                        total_active_power: float
                    }
                }]
        }
    }]
}

答案 1 :(得分:0)

好像你有多个嵌套对象。在您的情况下,如果您考虑将要取消的总数组获得1小时数据,它将是:1 * 60 * 60 = 3600。

此外,多个嵌套不必要地增加了检索和更新数据的复杂性。

您需要更扁平的结构,可以通过以下方式实现: -

为每分钟创建单独的文档。结构会像 -

{
 _id: ObjectId('');
 hour: 1,
 minute: 1
 seconds: [
  {
   item: 0,
   voltage_a: float,
   voltage_b: float,
   voltage_c: float,
   current_a: float,
   current_b: float,
   current_c: float,
   current_n: float,
   active_power_a: float,
   active_power_b: float,
   active_power_c: float,
   total_active_power: float
  },
  {
   item: 1,
   voltage_a: float,
   voltage_b: float,
   voltage_c: float,
   current_a: float,
   current_b: float,
   current_c: float,
   current_n: float,
   active_power_a: float,
   active_power_b: float,
   active_power_c: float,
   total_active_power: float
  }
 ]
},
{
 _id: ObjectId('');
 hour: 1,
 minute: 2
 seconds: [
  {
   item: 0,
   voltage_a: float,
   voltage_b: float,
   voltage_c: float,
   current_a: float,
   current_b: float,
   current_c: float,
   current_n: float,
   active_power_a: float,
   active_power_b: float,
   active_power_c: float,
   total_active_power: float
  },
  {
   item: 1,
   voltage_a: float,
   voltage_b: float,
   voltage_c: float,
   current_a: float,
   current_b: float,
   current_c: float,
   current_n: float,
   active_power_a: float,
   active_power_b: float,
   active_power_c: float,
   total_active_power: float
  }
 ]
}

此结构可能随后增加文档数量。但是会降低查询所涉及的复杂性。此外,通过有效的索引,可以保持性能。 (对于大数据,您还应该查看分片)。