2017-10-17 45 views
3

我有以下數據結構:

播放列表集合:

{ 
    _id:123, 
    artistId:959789, 
    title:'playlist1', 
    tracks:[{trackId:123456,createdDate:03.02.2017}, 
     {trackId:213556,createdDate:04.02.2017}, 
     {trackId:956125,createdDate:05.02.2017}] 
}, 
{ 
    _id:456, 
    artistId:456456, 
    title:'playlist2', 
    tracks:[{trackId:956336,createdDate:03.02.2017}, 
     {trackId:213556,createdDate:09.02.2017}, 
     {trackId:785556,createdDate:011.02.2017}] 
}, 
{ 
    _id:456, 
    artistId:456456, 
    title:'playlist3', 
    tracks:[{trackId:636985,createdDate:01.02.2017}, 
     {trackId:456585,createdDate:06.02.2017}, 
     {trackId:785556,createdDate:09.02.2017}] 
} 

trackId播放列表的曲目陣列中,在軌道中_id跟蹤收集

軌道集合:

{_id:956336,title:'abc'}, 
{_id:785556,title:'cdf'}, 
{_id:456585,title:'ghi'}, 
{_id:213556,title:'xyz'}, 
{_id:636985,title:'lmn'} 

我所做的就是聚集$lookuptracks陣列中使用trackId和我得到的結果。但playlistTracks在其他一些爲了不中tracks排列順序的排序。

{ 
     $match: {artistId: 456} 
}, 
{ 
    $lookup: { 
      from: 'tracks', 
      localField: 'tracks.trackId', 
      foreignField: '_id', 
      as: 'playlistTracks' 
     } 
}, 

現在我需要的是具有下列結構中的特定藝術家獲得播放列表的列表: 的playlistTracks應在訂貨的tracks數組排序的createdDate

{ 
    _id:456, 
    title:'playlist2', 
    tracks:[{trackId:636985,createdDate:01.02.2017}, 
     {trackId:456585,createdDate:06.02.2017}, 
     {trackId:785556,createdDate:09.02.2017}] 
    playlistTracks:[{_id:956336,title:'abc'}, 
      {_id:213556,title:'xyz'}, 
      {_id:785556,title:'cdf'}] 
}, 
{ 
    _id:456, 
    title:'playlist2', 
    tracks:[{trackId:636985,createdDate:01.02.2017}, 
     {trackId:456585,createdDate:06.02.2017}, 
     {trackId:785556,createdDate:09.02.2017}] 
    playlistTracks:[{_id:636985,title:'lmn'}, 
      {_id:456585,title:'ghi'}, 
      {_id:785556,title:'cdf'}] 
} 

回答

1

按照以下步驟

1 unwind the tracks array in playlist collection 
2 $lookup match with tracks collection 
3 add createddate of tracks array to lookup result as a new key 
4 sort based on new key 
5 group the results for your requirements 
+0

非常感謝。這有效! –

1

因此,這些都是我加的文件,重現你的使用情況:

播放列表收集

{ 
    "_id" : NumberInt(123), 
    "artistId" : NumberInt(959789), 
    "title" : "playlist1", 
    "tracks" : [ 
     { 
      "trackId" : NumberInt(123456), 
      "createdDate" : "03.02.2017" 
     }, 
     { 
      "trackId" : NumberInt(213556), 
      "createdDate" : "04.02.2017" 
     }, 
     { 
      "trackId" : NumberInt(956125), 
      "createdDate" : "05.02.2017" 
     } 
    ] 
} 
{ 
    "_id" : NumberInt(456), 
    "artistId" : NumberInt(456456), 
    "title" : "playlist2", 
    "tracks" : [ 
     { 
      "trackId" : NumberInt(956336), 
      "createdDate" : "03.02.2017" 
     }, 
     { 
      "trackId" : NumberInt(213556), 
      "createdDate" : "09.02.2017" 
     }, 
     { 
      "trackId" : NumberInt(785556), 
      "createdDate" : "11.02.2017" 
     } 
    ] 
} 
{ 
    "_id" : NumberInt(457), 
    "artistId" : NumberInt(456456), 
    "title" : "playlist3", 
    "tracks" : [ 
     { 
      "trackId" : NumberInt(636985), 
      "createdDate" : "01.02.2017" 
     }, 
     { 
      "trackId" : NumberInt(456585), 
      "createdDate" : "06.02.2017" 
     }, 
     { 
      "trackId" : NumberInt(785556), 
      "createdDate" : "09.02.2017" 
     } 
    ] 
} 

我_id改變了過去重複的_id播放列表收藏:457我不知道你怎麼能有相同的_id兩個文件。 _id字段必須是唯一的。而且我不知道我理解糾正你想要的結果,因爲在你的$match查詢您寫:$match: {artistId: 456}但在你的數據,因此沒有artiseId與。

,並在此日期

{的TrackID:785556,createdDate:011.02.2017}

從文件ID_ 456我改

{trackId:785556,createdDate:"11.02.2017"} 

原因之日起怪異的看了看。它也看起來像你的日期字段是字符串,因爲它看起來不像日期字段。無論哪種方式,$sort適用於這兩種用途。

軌道集合我留給你的榜樣。

因此,這似乎是你需要什麼?

db.playlist.aggregate([ 
{ 
     $match: {_id: {$in: [456]}} 
}, 
{ $unwind: "$tracks"}, 
{$sort: {"tracks.createdDate": 1}}, 
{ 
    $lookup: { 
      from: 'tracks', 
      localField: 'tracks.trackId', 
      foreignField: '_id', 
      as: 'playlistTracks' 
     } 
}, 
{ 
    $group:{ 
     _id: "$_id", 
     artistId: {$first: "$artistId"}, 
     title: {$first: "$title"}, 
     tracks: { $push: { item: "$tracks.trackId", quantity: "$tracks.createdDate" } }, 
     playlistTracks: { $push: "$playlistTracks" } 
    } 
} 
]) 

這使兩個數組到相同的順序。如果你想上升或下降-1以便您可以在這裏指定{$sort: {"tracks.createdDate": 1}}

因此要查找的字段,你可以放鬆和整理你的播放列表陣列之前。 希望這個作品

+0

其實我想'$ lookup'結果使用'createdDate'進行排序。如果我們這樣'$ unwind'和'$ sort','tracks'數組就會被排序。 –

+0

對不起,我誤解了你的問題,我會更新我的答案。 –

+0

非常感謝你的努力。我嘗試過這個。但沒有按照我想要的結果工作 –