参考我之前的问题,我有一个关于$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}
Emoji:
{_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
假设我在photo collection中添加另一个flag字段,现在我想只计数那些flag为1的照片。
我试图在查询$lookup之后立即添加$match,但它失败了,它不排除标志=0的照片,并且在计数器中也没有标志条件。
呈现输出10张照片中有3张照片设置了0号旗。我们不能考虑那些旗号为0的照片。所以期望的总数是7张照片,但是count返回10张照片,尽管我在照片中应用了flag条件。
当前查询:
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,
}
]
}
]
你不能在"$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"}
}
}
])
或者使用"$filter"
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"}
}
}
])
您的查询错误。localField:"album_emoji"
是什么,Album
集合中没有名为album_emoji
的字段。字段名是Album
集合中的album_id
,是Emoji
集合中_id
的外键。
如果您使用的是3.5版本,正确的查询是(我假设Album
, Photo
, Emoji
是三个集合的名称):
db.Album.aggregate([
{
$lookup:{
from:"Photo",
localField:"_id",
foreignField:"album_id",
as:"photo"
}
},
{
$match:{
"photo.flag": 1
}
},
{
$lookup:{
from:"Emoji",
localField:"emoji_id",
foreignField:"_id",
as:"emoji"
}
},
{
$project:{
album_name:"$album_name",
album_description:"$album_description",
album_emoji:"$emoji.emoji_name",
photo:"$photo",
total_photos: {$size: "$photo"}
}
}
]).pretty()
,结果如下:
/* 1 */
{
"_id" : 1,
"album_name" : "my album 1",
"album_description" : "album description 1",
"album_emoji" : [
"1.jpg"
],
"photo" : [
{
"_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
}
],
"total_photos" : 3
}