MongoDB - 查找 - 多个集合 - 结果在一个数组中

Posted

技术标签:

【中文标题】MongoDB - 查找 - 多个集合 - 结果在一个数组中【英文标题】:MongoDB - lookup - multiple collection - Result in one array 【发布时间】: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"
    ]
]

【问题讨论】:

【参考方案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

【讨论】:

以上是关于MongoDB - 查找 - 多个集合 - 结果在一个数组中的主要内容,如果未能解决你的问题,请参考以下文章

如何在两个集合之间建立查找连接

在 MongoDB 中查找丢失的集合

在 MongoDB 中查找丢失的集合

python进阶-MongoDB

MongoDB 和 rmongodb。获取查找的大小而不是返回所有结果

MongoDB 增删改查