6
我有深嵌套數組,我試圖通過一些嵌套數組元素進行分組,並得到這個工作。但是,當我嘗試使用$減法表達式時,它失敗。任何指針讚賞。mongodb聚合框架嵌套數組減去表達式
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沒有正確展開了$減,我試圖$項目,以減少管道等領域,並與$的各種組合發揮各地項目和$組失敗。也試圖放鬆不止一次,失敗。
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 }
我有一個很難理解你想要做什麼。你想要的輸出是什麼樣子? – JohnnyHK
期望的輸出是一個數字數組[響應時間,其中響應時間= timing.stop - timing.start]基於停止 - 開始時間陣列中的每個元素匹配給定timing.id例如。 SR-G。輸出是一個數組,因爲scenes.records是一個數組 – user1447121