2

有了我上一個問題的參考,我有一個關於添加一些條件的$ lookup的問題。您可以從以下鏈接描述中獲得足夠的有關問題的參考。

照片:

{_id: 1, photo_name: '1.jpg', photo_description: 'description 1', album_id: 1, flag:1 }, 
{_id: 2, photo_name: '2.jpg', photo_description: 'description 2', album_id: 1, flag:1 }, 
{_id: 3, photo_name: '3.jpg', photo_description: 'description 3', album_id: 1, flag:1 }, 
{_id: 4, photo_name: '4.jpg', photo_description: 'description 4', album_id: 2, flag:0 }, 
{_id: 5, photo_name: '5.jpg', photo_description: 'description 5', album_id: 2, flag:0 }, 
{_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

假設我在照片集合中添加了另一個標誌字段,現在我只想計算標誌爲1的那些照片。

我試圖在查詢中的$查找後立即添加$匹配,但它失敗,它不排除標誌爲0的照片,並且在計數器中它也不會標記條件。

本次輸出 有3張照片出來的10張照片將其具有標誌0。而且我們不能認爲這些照片,其標誌爲0,所以預計總數爲7張照片,但數返回10張照片,雖然我申請的條件國旗在照片中。

當前查詢:

db 
.album 
.aggregate([ 
    { 
    $lookup:{ 
     from:"photo", 
     localField:"_id", 
     foreignField:"album_id", 
     as:"photo" 
    } 
    }, 
    { 
    $match:{ 
     "photo.flag": 1 
    } 
    }, 
    { 
    $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", 
     total_photos: {$size: "$photo"} 
    } 
    } 
]) 
.toArray(); 

預期輸出:

[ 
    { 
     "_id" : 1, 
     "album_name" : "Album 1", 
     "album_description" : "Album description 1", 
     "album_emoji" : [ 
      "1.jpg" 
     ], 
     "total_photos" : 3, 
     "photo" : [ 
      { 
       "_id" : 1, 
       "album_id" : 1, 
       "photo_description" : "description 1", 
       "photo_name" : "1.jpg",    
       "flag" : 0, 
      }, 
      { 
       "_id" : 2, 
       "album_id" : 1, 
       "photo_description" : "description 2", 
       "photo_name" : "2.jpg",    
       "flag" : 0, 
      }, 
      { 
       "_id" : 1, 
       "album_id" : 1, 
       "photo_description" : "description 3", 
       "photo_name" : "3.jpg",    
       "flag" : 0, 
      } 
     ] 
    } 
] 

當前輸出:

[ 
    { 
     "_id" : 1, 
     "album_name" : "Album 1", 
     "album_description" : "Album description 1", 
     "album_emoji" : [ 
      "1.jpg" 
     ], 
     "total_photos" : 5, 
     "photo" : [ 
      { 
       "_id" : 1, 
       "album_id" : 1, 
       "photo_description" : "description 1", 
       "photo_name" : "1.jpg",    
       "flag" : 1, 
      }, 
      { 
       "_id" : 2, 
       "album_id" : 1, 
       "photo_description" : "description 2", 
       "photo_name" : "2.jpg",    
       "flag" : 1, 
      }, 
      { 
       "_id" : 3, 
       "album_id" : 1, 
       "photo_description" : "description 3", 
       "photo_name" : "3.jpg",    
       "flag" : 1, 
      }, 
      { 
       "_id" : 4, 
       "album_id" : 1, 
       "photo_description" : "description 4", 
       "photo_name" : "4.jpg",    
       "flag" : 0, 
      }, 
      { 
       "_id" : 5, 
       "album_id" : 1, 
       "photo_description" : "description 5", 
       "photo_name" : "5.jpg",    
       "flag" : 0, 
      } 
     ] 
    } 
] 
+0

它確實排除了「photo.flag」爲0的照片。你在使用MongoDB 3.2嗎? – notionquest

+0

是的,我正在使用mongoDB 3.2 –

+0

@notionquest:是的我使用MongoDB3.2,也是這個查詢工作我..但對我來說,它不排除recods,並沒有給出總的照片計數器的正確結果。 –

回答

1

「$ lookup」後面的對象方法不能使用「$ match」,因爲「$ lookup」的返回值是數組值。查找之後,最好添加「$ unwind」功能,然後對其進行分組。

實例查詢

db 
.album 
.aggregate([ 
    { 
     $lookup:{ 
      from:"photo", 
      localField:"_id", 
      foreignField:"album_id", 
      as:"photo" 
     } 
    }, 
    { 
     preserveNullAndEmptyArrays : true, 
     path : "$photo" 
    }, 
    { 
     $match:{ 
      "photo.flag": 1 
     } 
    }, 
    { 
     $group : { 
      _id : { 
       id : "$_id", 
       album_name: "$album_name", 
       album_description: "$album_description", 
       emoji_id: "$emoji_id" 
      }, 
      photo: { 
       $push : "$photo" 
      } 
     } 
    } 
    { 
     $lookup:{ 
      from:"emoji", 
      localField:"_id.album_emoji", 
      foreignField:"_id", 
      as:"emoji" 
     } 
    }, 
    { 
     $project:{ 
      album_name:"$album_name", 
      album_description:"$album_description", 
      emoji:"$emoji", 
      photo:"$photo", 
      total_photos: {$size: "$photo"} 
     } 
    } 
]) 

;或用 「$過濾器」。

db 
.album 
.aggregate([ 
    { 
     $lookup:{ 
      from:"photo", 
      localField:"_id", 
      foreignField:"album_id", 
      as:"photo" 
     } 
    }, 
    { 
     $project: { 
      id : "$_id", 
      album_name: "$album_name", 
      album_description: "$album_description", 
      emoji_id: "$emoji_id", 
      photo: { 
       $filter : { 
        input: "$photo", 
        as : "photo_field", 
        cond : { 
         $eq: ["$$photo_field.flag",1] 
        } 
       } 
      } 
     } 
    }, 
    { 
     $lookup:{ 
      from:"emoji", 
      localField:"album_emoji", 
      foreignField:"_id", 
      as:"emoji" 
     } 
    }, 
    { 
     $project:{ 
      album_name:"$album_name", 
      album_description:"$album_description", 
      emoji:"$emoji", 
      photo:"$photo", 
      total_photos: {$size: "$photo"} 
     } 
    } 
]) 
+0

您在第二階段缺少'$ unwind'定義。 –

相關問題