Node.js mongodb聚合框架嵌套数组减法表达式
我有深层嵌套数组,我正试图通过一些嵌套数组元素进行分组,并使其正常工作。然而,当我尝试使用$subtract表达式时,它失败了。欢迎指点Node.js mongodb聚合框架嵌套数组减法表达式,node.js,mongodb,aggregation-framework,Node.js,Mongodb,Aggregation Framework,我有深层嵌套数组,我正试图通过一些嵌套数组元素进行分组,并使其正常工作。然而,当我尝试使用$subtract表达式时,它失败了。欢迎指点 Data: -scenes: [ -{ name: "Greeting_Excited" -records: [ - { type: "listeningCycle" listeningId: 2 timestamp: 135456
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 }
及
两个问题:
$unwind
每个嵌套数组级别一直到计时
$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 ] } ]
我很难理解你想做什么。您想要的输出是什么样子的?想要的输出是一个数字数组[responsetimes,其中responsetimes=timing.stop-timing.start],基于与给定timing.id匹配的计时数组中每个元素的停止-开始,例如SR-G。输出是一个数组,因为scenes.records是一个数组
[ { _id: 'Greeting_Excited', responseTimes: [ 156 ] } ]