如何在MongoDB中嵌套$ lookup搜索?

时间:2018-10-09 01:48:00

标签: mongodb aggregation-framework

我有3个收藏集:

  1. 职位:
+------------------+----------------------+-----------------------+
|    position_id   |    company_id        |       position_name   |
+------------------+----------------------+-----------------------+
|         1        |        1             |        position 1     |
+------------------+----------------------+-----------------------+
|         2        |        2             |        position 2     |
+------------------+----------------------+-----------------------+
  1. 公司:
+------------------+----------------------+-----------------------+
|    company_id    |     industry_id      |       company_name    |
+------------------+----------------------+-----------------------+
|        1         |          1           |       company 1       |
+------------------+----------------------+-----------------------+
|        2         |          2           |       company 2       |
+-----------------------------------------------------------------+
  1. 行业:
+------------------+----------------------+
|     industry_id  |       industry_name  |
+------------------+----------------------+
|          1       |      industry 1      |
+------------------+----------------------+
|          2       |      industry 2      |
+------------------+----------------------+

我需要在一个API中返回以下结果:

[{
  position_id: 1,
  position_name: 'position 1',
  company: {
    company_id: 1,
    company_name: 'company 1',
    industry: {
      industry_id: 1,
      industry_name: 'industry 1',
    }
  }
}, {
  position_id: 2,
  posiiton_name: 'position 2',
  company: {
    company_id: 2,
    company_name: 'company 2',
    industry: {
      industry_id: 2,
      industry_name: 'industry 2',
    }
  }
}]

所以我能想到的管道部分的代码如下:

const pipelines = [{
  $lookup: {
    from: 'companies',
    localField: 'company_id',
    foreignField: 'company_id',
    as: 'company',

    $lookup: {
      from: 'industries',
      localField: 'industry_id',
      foreignField: 'industry_id',
      as: 'industry'
    }
  }
}]

return positions.aggregate(pipelines);

但这会引发一些错误。那么在mongodb搜索中执行嵌套$ lookup的正确方法是什么?

谢谢!

1 个答案:

答案 0 :(得分:3)

$lookup 3.6 语法允许您联接嵌套表,并使用$unwind从输入文档中解构数组字段,以输出每个元素的文档。像这样

position.aggregate([
  { "$lookup": {
    "from": "companies",
    "let": { "companyId": "$company_id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$_id", "$$companyId" ] } } },
      { "$lookup": {
        "from": "industries",
        "let": { "industry_id": "$industry_id" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": [ "$_id", "$$industry_id" ] } } }
        ],
        "as": "industry"
      }},
      { "$unwind": "$industry" }
    ],
    "as": "company"
  }},
  { "$unwind": "$company" }
])

使用3.4版本

position.aggregate([
  { "$lookup": {
    "from": "companies",
    "localField": "company_id",
    "foreignField": "_id",
    "as": "companies"
  }},
  { "$unwind": "$companies" },
  { "$lookup": {
    "from": "industries",
    "localField": "companies.industry_id",
    "foreignField": "_id",
    "as": "companies.industry"
  }},
  { "$unwind": "$companies.industry" },
  { "$group": {
    "_id": "$_id",
    "companies": { "$push": "$companies" }
  }}
])