我正在尝试从查询下面的集合表单中获取数据:
db.getCollection('jobs').aggregate(
{$match :{"slug":"bath-room-designer-for-whole-floor-772000"}},
{$unwind: "$job_activity"},
{$lookup: {
"from":"users",
"localField":"job_activity.user_id",
"foreignField":"_id",
"as": "user_details"
}
},
{$unwind: { path: "$user_details", preserveNullAndEmptyArrays: false } },
{$group: {
"_id": "$_id",
"Job_detail": {"$push": "$job_activity"},
"job_activity": {"$push": "$job_activity"},
"user_details": {"$push": "$user_details"}
}
}
)
但是,如果$ match的集合中存在datat,则会导致空数据,但此slug不存在job_activity
任何人都可以提供帮助 感谢
答案 0 :(得分:9)
使用$unwind的 preserveNullAndEmptyArrays 来保存job_activity
不存在或为空的文档
所以请使用 $unwind
阶段:
{
$unwind:
{
path: "$job_activity",
preserveNullAndEmptyArrays: true
}
}