今天有一個業務涉及到mongodb的多表查詢,大致記錄下語句結構mongodb
db.table_a.aggregate([code
{$lookup:{from:"table_b",localField:"userid",foreignField:"userid",as:"organization_doc"}}, //聯表B
{
$project:{
_id:1,
cardid:1,
checkin_time:1,
checkin_date:1,
userid:1,
selfcare_ability:1,
checkout_time:1,
checkout_date:1,
inventory_doc:"$organization_doc.name"
} //經過project參數限制返回數據,提升效率
},
{$lookup:{from:"table_c",localField:"cardid",foreignField:"cardid",as:"archives_doc"}}, //聯表c排序
{$match:{"archives_doc.xingzheng_code" : "370213000001"}}, //匹配搜索條件condition
{$limit:10},{$skip:1}]) //其餘參數,如條數顯示,翻頁,排序等ip