【问题标题】:MongoDB - lookup - multiple collection - Result in one arrayMongoDB - 查找 - 多个集合 - 结果在一个数组中
【发布时间】:2021-07-23 14:55:30
【问题描述】:

网站集字段 - _id, name

节点集合字段 - _id, siteId, name

设备集合字段 - _id, nodeId, name

传感器收集字段 - _id, deviceId, name

我是 MongoDB 新手,预期结果(没有重复数据):

[{
        "_id": "608aa9bd323489617cfe2081",
        "name": "Site One two 3",
        "node": [{
            "name": "Node Guj 3 222 ",
            "device": [{
                "mode": 1,
                "siteId": "608aa9bd323489617cfe2081",
                "nodeId": "608aa9cc323489617cfe2083",
                "isActive": true,
                "_id": "608aa9ee323489617cfe2084",
                "sensor": [{
                    "_id": "608aa9ee323489617cfe2085",
                    "name": "NVR_Channel1"
                }]
            }]
        }]
    },
    {
        "_id": "608aa9bd323489617cfe2083",
        "name": "Site One two 22",
        "node": [{
            "name": "Node Guj 3 222 ",
            "device": []
        }]
    },
    {
        "_id": "608aa9bd323489617cfe2085",
        "name": "SiteO",
        "node": [{
            "name": "Node22 ",
            "device": [{
                "mode": 1,
                "siteId": "608aa9bd323489617cfe2081",
                "nodeId": "608aa9cc323489617cfe2083",
                "isActive": true,
                "_id": "608aa9ee323489617cfe2084",
                "sensor": []
            }]
        }]
    }
]

正如我之前提到的,我是 MongoDB 的新手,我尝试了以下查询,但没有得到预期的结果

const result = await Site.aggregate([
        {
          $lookup:
          {
            from: "nodes",
            localField: "_id",
            foreignField: "siteId",
            as: "node"
          }
        },
        {
          $lookup:
          {
            from: "devices",
            localField: "node._id",
            foreignField: "nodeId",
            as: "device"
          }
        },
        {
          $lookup:
          {
            from: "sensors",
            localField: "node.device._id",
            foreignField: "deviceId",
            as: "sensor"
          }
        }
      ]);

以下是当前响应,其中nodedevicesensor 处于同一级别,预期是站点 -> 节点 -> 设备 -> 传感器。

[{
    "_id": "608aab1016be1c11dfe77422",
    "name": "Aivid_Site_One",
    "node": [{
            "_id": "608b9072932b3c0cc5ab4fd0",
            "name": "no sensort 11"
        },
        {
            "_id": "608b9073932b3c0cc5ab4fd1",
            "name": "no sensort 11"
        }
    ],
    "device": [{
        "_id": "608b9091932b3c0cc5ab4fd2",
        "name": "3nd floor Device"
    }],
    "sensor": [{
        "_id": "608b9091932b3c0cc5ab4f33",
        "name": "sensor 1"
    }]
}]

【问题讨论】:

    标签: mongodb mongoose mongodb-query mongodb-lookup


    【解决方案1】:

    您可以尝试使用管道查找,

    • $lookup 带节点并将siteId 传递给管道
    • $matchsiteId条件
    • $lookupdevices 并将 deviceId 传递给管道
    • $matchdeviceId条件
    • $lookupsensors 合集
    db.sites.aggregate([
      {
        $lookup: {
          from: "nodes",
          let: { siteId: "$_id" },
          pipeline: [
            { $match: { $expr: { $eq: ["$$siteId", "$siteId"] } } },
            {
              $lookup: {
                from: "devices",
                let: { nodeId: "$_id" },
                pipeline: [
                  { $match: { $expr: { $eq: ["$$nodeId", "$nodeId"] } } },
                  {
                    $lookup: {
                      from: "sensors",
                      localField: "_id",
                      foreignField: "deviceId",
                      as: "sensors"
                    }
                  }
                ],
                as: "devices"
              }
            }
          ],
          as: "nodes"
        }
      }
    ])
    

    Playground

    【讨论】:

      猜你喜欢
      • 2016-06-07
      • 1970-01-01
      • 2014-07-07
      • 2019-12-30
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多