2016-09-23 45 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 
    } 
] 
} 

此結構可能隨後增加文檔的數量。但會降低查詢所涉及的複雜性。而且,通過高效的索引,可以保持性能。 (對於大數據,你也應該看分片)。