我有一個存儲在mongodb中的記錄數據集,我一直在試圖從記錄中提取一組複雜的數據。彙總輸出到Nest Arrays
記錄樣本如下: -
{
bookId : '135wfkjdbv',
type : 'a',
store : 'crossword',
shelf : 'A1'
}
{
bookId : '13erjfn',
type : 'b',
store : 'crossword',
shelf : 'A2'
}
我一直在試圖提取數據,使得對於每個BOOKID,我得到一個數(記錄),用於保存標識的圖書每家店的名字每個貨架通過bookId這本書的類型是'a'。
據我所知,聚合查詢允許一個管道,允許分組,匹配等,但我一直無法達成解決方案。
所需的輸出的形式爲: - 當你看到在
{
bookId : '135wfkjdbv',
stores : [
{
name : 'crossword'
shelves : [
{
name : 'A1',
count : 12
},
]
},
{
name : 'granth'
shelves : [
{
name : 'C2',
count : 12
},
{
name : 'C4',
count : 12
},
]
}
]
}
謝謝你的詳細解答:) – ContinuousError