[英]mongodb aggregation framework nested arrays subtract expression
我有深層嵌套數組,我試圖通過一些嵌套數組元素進行分組,並使其工作。 但是,當我嘗試使用$ 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 }
夫妻問題:
$unwind
每個嵌套陣列級別,通過一路timing
$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 ] } ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.