7

我有深层嵌套数组,我试图按一些嵌套数组元素进行分组并让它工作。但是,当我尝试使用 $subtract 表达式时,它失败了。任何指针表示赞赏。

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 没有为 $subtract 正确展开,我尝试 $project 来减少管道中的字段,并尝试使用 $project 和 $group 的各种组合失败。也不止一次尝试放松,没有成功。

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

1 回答 1

10

几个问题:

  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 ] } ]
于 2012-12-05T03:32:40.447 回答