2016-09-23 139 views
1

我有一个集合,其中每个文件看起来是这样的: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")} 
     } 
    } 

    ] 

); 

是否有可能“开卷”的文件 - 类似你如何放松身心的阵列,这样我可以公开每个嵌套层的一个对象?

+0

使用'$ unwind'操作符不能'展开'文档,它仅适用于数组。 – chridam

+0

我知道...这就是为什么我正在寻找这种情况下的等价物/替代物。 –

回答

2

您应该已经创建了模式在下列方式:

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

i.e.your数据应该是objects.and从数组的数组,你可以使用它的索引抓取任何对象。

{ 
    _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 
        } 
       }] 
     } 
    }] 
} 
+0

这看起来很不错!将尝试它。 –

+0

@AbdulMaye。很高兴听到它帮助..不要忘记标记答案,如果它工作正常。干杯。 – Sachin

+0

所以看起来这可能会简化查询过程,但我将如何去实际更新这些记录?假设我想追加具有'name:1'的对象的info数组。我使用'updateOne'和'upsert:true'。 –

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 
    } 
] 
} 

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