2016-08-14 58 views
4

以下是我的收藏架構。mongodb如何在分類查詢中應用分頁和總照片數量

我不知道如何應用分頁並在總計查詢中獲得總照片數量。我知道這是可能的,但我嘗試了很多,但我沒有解決問題。指導我解決以下問題。

如果有任何優化的解決方案,請指導我。

照片:

{_id: 1, photo_name: '1.jpg', photo_description: 'description 1', album_id: 1}, 
{_id: 2, photo_name: '2.jpg', photo_description: 'description 2', album_id: 1}, 
{_id: 3, photo_name: '3.jpg', photo_description: 'description 3', album_id: 1}, 
{_id: 4, photo_name: '4.jpg', photo_description: 'description 4', album_id: 2}, 
{_id: 5, photo_name: '5.jpg', photo_description: 'description 5', album_id: 2}, 
{_id: 6, photo_name: '6.jpg', photo_description: 'description 6', album_id: 2} 

專輯:

{_id: 1, album_name: "my album 1", album_description: "album description 1", emoji_id: 1}, 
{_id: 2, album_name: "my album 2", album_description: "album description 2", emoji_id: 2}, 
{_id: 3, album_name: "my album 3", album_description: "album description 3", emoji_id: 3}, 
{_id: 4, album_name: "my album 4", album_description: "album description 4", emoji_id: 4}, 
{_id: 5, album_name: "my album 5", album_description: "album description 5", emoji_id: 5} 

表情:

{_id: 1, emoji_name: "1.jpg"}, 
{_id: 2, emoji_name: "2.jpg"}, 
{_id: 3, emoji_name: "3.jpg"}, 
{_id: 4, emoji_name: "4.jpg"}, 
{_id: 5, emoji_name: "5.jpg"}, 
{_id: 6, emoji_name: "6.jpg"}, 
{_id: 7, emoji_name: "7.jpg"}, 
{_id: 8, emoji_name: "8.jpg"} 

測試記錄分頁:

2

預期輸出:

[ 
    { 
     album_id: 1, 
     album_name: "my album 1", 
     album_emoji: "1.jpg", 
     total_photos: 3,(total photo counts of all photos of paritcular album) 
     photos: [ 
      { 
       {_id: 1, photo_name: '1.jpg', photo_description: 'description 1'}, 
       {_id: 2, photo_name: '2.jpg', photo_description: 'description 2'} 
      } 
     ] 
    } 
] 

當前查詢:

db 
.album 
.aggregate([ 
    { 
    $lookup:{ 
     from:"photo", 
     localField:"_id", 
     foreignField:"album_id", 
     as:"photo" 
    } 
    }, 
    { 
    $lookup:{ 
     from:"emoji", 
     localField:"album_emoji", 
     foreignField:"_id", 
     as:"emoji" 
    } 
    }, 
    { 
    $project:{ 
     album_name:"$album_name", 
     album_description:"$album_description", 
     album_emoji:"$emoji.image_name", 
     photo:"$photo" 
    } 
    }, 
    { 
    $match:{ 
     _id: 1 
    } 
    } 
]) 
.toArray(); 

本次輸出:

[{ 
    "_id" : 1, 
    "album_name" : "my album 1", 
    "album_emoji" : [ 
     "1.png" 
    ], 
    "photo" : [ 
     {_id: 1, photo_name: '1.jpg', photo_description: 'description 1', album_id: 1}, 
     {_id: 2, photo_name: '2.jpg', photo_description: 'description 2', album_id: 1}, 
     {_id: 3, photo_name: '3.jpg', photo_description: 'description 3', album_id: 1}, 
    ] 
}] 

回答

1

您可能要檢查$slice和調整$project一些階段什麼樣的這個

... 
$project:{ 
    album_name:"$album_name", 
    album_description:"$album_description", 
    album_emoji:"$emoji.image_name", 
    photo: { $slice: [ "$photo", 0, 2 ] } 
} 
... 

只是通過不同的值到$slice運營商獲得不同的頁面

+0

但如果我想獲得的總記錄數? –

+2

您可以在'$ project'階段的'photo'數組中使用[**'$ size' **](https://docs.mongodb.com/manual/reference/operator/aggregation/size/)數組聚合運算符。 – tarashypka

+0

它正在工作並解決我的問題。謝謝大家。 –