mongodb - 如何在 MongoDB 中进行嵌套 $lookup 搜索?

标签 mongodb mongodb-query aggregation-framework

我有 3 个集合:

  1. 职位:
+------------------+----------------------+-----------------------+
|    position_id   |    company_id        |       position_name   |
+------------------+----------------------+-----------------------+
|         1        |        1             |        position 1     |
+------------------+----------------------+-----------------------+
|         2        |        2             |        position 2     |
+------------------+----------------------+-----------------------+
  1. companies:
+------------------+----------------------+-----------------------+
|    company_id    |     industry_id      |       company_name    |
+------------------+----------------------+-----------------------+
|        1         |          1           |       company 1       |
+------------------+----------------------+-----------------------+
|        2         |          2           |       company 2       |
+-----------------------------------------------------------------+
  1. industries:
+------------------+----------------------+
|     industry_id  |       industry_name  |
+------------------+----------------------+
|          1       |      industry 1      |
+------------------+----------------------+
|          2       |      industry 2      |
+------------------+----------------------+

I need to return the following result in one 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',
    }
  }
}]

所以我能想到的pipeline部分的代码是这样的:

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 的正确方法是什么?

提前致谢!

最佳答案

$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" }
  }}
])

关于mongodb - 如何在 MongoDB 中进行嵌套 $lookup 搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712240/

相关文章:

javascript - 如何比较 Mongoose 对象和 JSON 对象?

带有聚合查询的 mongoDb $in

mongodb - 如何从 mongodb 的文档中只删除一两个字段?

Go 中的 MongoDB 聚合查找 (mgo.v2)

mongodb - Mongo : Find documents that have 0 associated documents, 更便宜

java - 将 AggregationOutput 转换为 JsonObject

node.js - 如何使用 Mongoose 删除数据库?

javascript - MongoDB Cursor.each 抛出错误

mongodb - 如何在mongoDB中使用通配符字段?

java - JDK 17 spring boot 无法使 java.time.LocalDateTime 私有(private)化