如何在mongoDB中编写连接查询..?

时间:2017-07-27 09:31:02

标签: node.js mongodb mongoose

对于那个mysql查询的mongoDB查询是怎么在mongoDB中使用join的,我面临的问题是在mysql中编写?

SELECT t.uid, count(j.job_id) AS x 
FROM verify AS t 
LEFT JOIN jobs AS j 
ON t.uid = j.job_id

1 个答案:

答案 0 :(得分:1)

如下所示,使用聚合框架$project& $lookup管道:

db.collection1.aggregation[{
    $project: {

        'uid': 1,
        "x": {
            "$size": "job_id"
        }
    },

    $lookup: {
        from: 'collection2',
        localField: 'uid',
        foreignField: 'job_id',
        as: 'j'
    }
}]