+
95
-

mongodb如何聚合连表查询?

mongodb如何聚合连表查询?

网友回复

+
15
-

可以使用mongodb的高级查询$lookup

{
   $lookup:
     {
       from: <collection to join>,
       localField: <field from the input documents>,
       foreignField: <field from the documents of the "from" collection>,
       as: <output array field>
     }
}

其实换成相当于sql语句的join用法

SELECT *, <output array field>
FROM collection
WHERE <output array field> IN (SELECT *
                               FROM <collection to join>
                               WHERE <foreignField> = <collection.localField>);

我们以php为例,来编写一个$lookup的聚合查询连表多表示例代码:

<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017...

点击查看剩余70%

我知道答案,我要回答