[英]mongodb aggregation framework nested arrays subtract expression
i have deep nested arrays, i am trying to group by some nested array elements and got this to work. 我有深层嵌套数组,我试图通过一些嵌套数组元素进行分组,并使其工作。 however, when i try to use $subtract expression, it fails.
但是,当我尝试使用$ subtract表达式时,它会失败。 any pointers appreciated.
任何指针赞赏。
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
}
it seems that the innermost nested array: $scenes.records.timeProfile.timing is not unwound correctly for $subtract, i tried $project to reduce the fields in the pipeline and played around with various combinations of $project and $group unsuccessfully. 似乎最里面的嵌套数组:$ scenes.records.timeProfile.timing没有正确解开$ subtract,我尝试使用$ project来减少管道中的字段并使用$ project和$ group的各种组合来解决这个问题。 also tried to unwind more than once, unsuccessfully.
也试图不止一次放松,但未成功。
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 }
and 和
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 }
Couple problems: 夫妻问题:
$unwind
each nested array level all the way through to timing
$unwind
每个嵌套阵列级别,通过一路timing
$subtract
is used with $project
, not $group
$subtract
与$project
,而不是$group
Try this: 尝试这个:
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);
});
Outputs: 输出:
[ { _id: 'Greeting_Excited', responseTimes: [ 156 ] } ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.