背景:
- 目的验证数据是否入到相关库中
- 手动方法:先在person表中,找到face_id;拿fece_id到feature_repository表中,找到对应的feature_id,以及repo_id;最后验证是否有feature_id,repo_id是否是3
连表查询,一次查询到位,不用多个表手动copy:
-
连表查询,feature_repository left join perso
db.feature_repository.aggregate([{ $lookup:{ from:"person", localField:"feature_id", foreignField:"face_list.face_id", as:"f" } } ])
- 拆分:把有多个face_id的person ,拆分成多个record
db.feature_repository.aggregate([{ $lookup:{ from:"person", localField:"feature_id", foreignField:"face_list.face_id", as:"f" } },{ $unwind: "$f" } ])
- 在一级关系中,新增字段name,便于后面展示
db.feature_repository.aggregate([{ $lookup:{ from:"person", localField:"feature_id", foreignField:"face_list.face_id", as:"f" } },{ $unwind: "$f" },{ $addFields:{name: "$f.person_information.name"} } ])
- 展示想要的字段,1展示 0不展示
db.feature_repository.aggregate([{ $lookup:{ from:"person", localField:"feature_id", foreignField:"face_list.face_id", as:"f" } },{ $unwind: "$f" },{ $addFields:{name: "$f.person_information.name"} },{ $project:{ feature_id:1, name:1 } } ])