2012-12-04 111 views
6

我有深嵌套數組,我試圖通過一些嵌套數組元素進行分組,並得到這個工作。但是,當我嘗試使用$減法表達式時,它失敗。任何指針讚賞。mongodb聚合框架嵌套數組減去表達式

Data: 

-scenes: [ 
    -{ 
    name: "Greeting_Excited" 
    -records: [ 
      - { 
       type: "listeningCycle" 
       listeningId: 2 
       timestamp: 1354566662041 
       -events: [ … ] 
       -timeProfile: { 
       -timeStampInfo: { 
       earliestStamp: 1354566664530 
       latestStamp: 1354566678412 
       } 
       -timing: [ 
        -{ 
         start: 400 
         stop: 556 
         id: "SR-G" 
        } 
       -{ 
         start: 559 
         stop: 572 
         id: "NL-G" 
        } 
       ] 
       } 
       } 
      ] 
      } 
     ] 

collection..aggregate({$unwind:"$scenes"}, {$match: {'scenes.records.timeProfile.timing.id' : 'SR-G'}}, {$group: { _id : {segmentname: "$scenes.name"} , responsetimes : { $push : {$subtract : ["$scenes.records.timeProfile.timing.stop", "$scenes.records.timeProfile.timing.start"]} }}}, {$sort:{responsetimes:1}} 

I am using the mongodb 2.2.1 and native node mongodb driver 1.1.11. 

what i am trying to do: 
    -group by scenes [unwind the scenes array], 
    -for a match with SR-G timing-id, 
    -gather all the response times, hence the $subtract (stop-start). 

This is the error msg i see: 

{ 
"errmsg" : "exception: can't convert from BSON type Array to long", 
"code" : 16004, 
"ok" : 0 
} 

似乎內部嵌套數組:$ scenes.records.timeProfile.timing沒有正確展開了$減,我試圖$項目,以減少管道等領域,並與$的各種組合發揮各地項目和$組失敗。也試圖放鬆不止一次,失敗。

collection.aggregate({$project: {segmentname:"$scenes.name", timingid: "$scenes.records.timeProfile.timing.id", timingstart:"$scenes.records.timeProfile.timing.start", timingstop:"$scenes.records.timeProfile.timing.stop"}}, {$unwind:"$scenes"}, {$match: {'scenes.records.timeProfile.timing.id' : 'SR-G'}}) 
{ "result" : [ ], "ok" : 1 } 

collection.aggregate({$unwind: "$scenes"}, {$project: {segmentname:"$scenes.name", timingid: "$scenes.records.timeProfile.timing.id", timingstart:"$scenes.records.timeProfile.timing.start", timingstop:"$scenes.records.timeProfile.timing.stop"}}, {$unwind:"$scenes.records.timeProfile.timing"}, {$match: { "scenes.records.timeProfile.timing.id" : "SR-G"}}, {$project: {segmentname:"$scenes.name", timingid: "$scenes.records.timeProfile.timing.id", timingstart:"$scenes.records.timeProfile.timing.start", timingstop:"$scenes.records.timeProfile.timing.stop"}}) 
{ "result" : [ ], "ok" : 1 } 
+0

我有一個很難理解你想要做什麼。你想要的輸出是什麼樣子? – JohnnyHK

+0

期望的輸出是一個數字數組[響應時間,其中響應時間= timing.stop - timing.start]基於停止 - 開始時間陣列中的每個元素匹配給定timing.id例如。 SR-G。輸出是一個數組,因爲scenes.records是一個數組 – user1447121

回答

9

夫婦的問題:

  1. 您需要$unwind每個嵌套陣列級一路走過來timing
  2. $subtract使用與$project,不$group

試試這個:

collection.aggregate([ 
    {$unwind: "$scenes"}, 
    {$unwind: "$scenes.records"}, 
    {$unwind: "$scenes.records.timeProfile.timing"}, 
    {$match: {'scenes.records.timeProfile.timing.id' : 'SR-G'}}, 
    {$project: { 
     segmentname: "$scenes.name", 
     responseTime: {$subtract: ['$scenes.records.timeProfile.timing.stop', '$scenes.records.timeProfile.timing.start'] } 
    }}, 
    {$group: { 
     _id: '$segmentname', 
     responseTimes: {$push: '$responseTime'} 
    }} 
], function (err, results) { 
    console.log(results); 
}); 

輸出:

[ { _id: 'Greeting_Excited', responseTimes: [ 156 ] } ] 
+0

謝謝,這工作。 – user1447121