Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – mongodb聚合框架嵌套数组减去表达式大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有深层嵌套数组,我试图通过一些嵌套数组元素进行分组,并使其工作.但是,当我尝试使用$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",{$unwind:"$scenes.records.timeProfile.timing"},{$match: { "scenes.records.timeProfile.timing.id" : "SR-G"}},timingstop:"$scenes.records.timeProfile.timing.stop"}} )
{ "result" : [ ],"ok" : 1 }

解决方法

夫妻问题:

>您需要将每个嵌套数组级别一直展开到计时
> $subtract与$project一起使用,而不是$group

试试这个:

collection.aggregate([
    {$unwind: "$scenes"},{$unwind: "$scenes.records"},{$unwind: "$scenes.records.timeProfile.timing"},{$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 ] } ]

大佬总结

以上是大佬教程为你收集整理的node.js – mongodb聚合框架嵌套数组减去表达式全部内容,希望文章能够帮你解决node.js – mongodb聚合框架嵌套数组减去表达式所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。