You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mongo聚合嵌套$lookup失效:如何填充userInfo字段?

解决MongoDB聚合查询中userInfo字段为空的问题

我尝试使用MongoDB的aggregate进行嵌套$lookup查询,关联订单对应的店铺所属用户(Order->Shop->User),但聚合查询返回的userInfo字段为空。

数据库结构

db={
  "orders": [
    {
      "_id": 1,
      "shop": 1,
      "price": 11
    },
    {
      "_id": 2,
      "shop": 2,
      "price": 101
    },
  ],
  "shops": [
    {
      "_id": 1,
      "user": "2"
    },
    {
      "_id": 2,
      "user": "1"
    },
    
  ],
  "users": [
    {
      "_id": 1,
      "country": "US"
    },
    {
      "_id": 2,
      "country": "UK"
    }
  ]
}

原聚合查询代码

db.orders.aggregate([
  {
    $lookup: {
      from: "shops",
      let: {
        shop: "$_id"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $eq: [
                "$_id",
                "$$shop"
              ]
            },
            
          },
          
        },
        
      ],
      as: "shopInfo",
      
    },
    
  },
  {
    $unwind: "$shopInfo"
  },
  {
    $lookup: {
      from: "users",
      let: {
        user: "$shopInfo.user"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $eq: [
                "$_id",
                "$$user"
              ]
            },
            
          },
          
        },
        
      ],
      as: "userInfo",
      
    },
    
  },
  
])

原查询结果

[
  {
    "_id": 1,
    "price": 11,
    "shop": 1,
    "shopInfo": {
      "_id": 1,
      "user": "2"
    },
    "userInfo": []
  },
  {
    "_id": 2,
    "price": 101,
    "shop": 2,
    "shopInfo": {
      "_id": 2,
      "user": "1"
    },
    "userInfo": []
  }
]

问题分析与修复

出现这个问题有两个关键原因:

1. 第一个$lookup的关联字段错误

原代码中第一个$lookup的let变量使用了shop: "$_id",但订单表(orders)中关联店铺的字段是shop而非_id,这会导致关联逻辑错误。

2. 数据类型不匹配

店铺表(shops)中的user字段是字符串类型(如"2"),而用户表(users)中的_id是数字类型(如2),$eq比较时会严格校验类型,类型不匹配则无法匹配到数据。

修正后的聚合查询代码

db.orders.aggregate([
  {
    $lookup: {
      from: "shops",
      let: {
        shopId: "$shop" // 修正:引用orders的shop字段作为关联ID
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $eq: ["$_id", "$$shopId"]
            }
          }
        }
      ],
      as: "shopInfo"
    }
  },
  { $unwind: "$shopInfo" },
  {
    $lookup: {
      from: "users",
      let: {
        userId: "$shopInfo.user"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $eq: ["$_id", { $toInt: "$$userId" }] // 转换字符串为数字,匹配users的_id类型
            }
          }
        }
      ],
      as: "userInfo"
    }
  }
])

修正后的查询结果

[
  {
    "_id": 1,
    "price": 11,
    "shop": 1,
    "shopInfo": { "_id": 1, "user": "2" },
    "userInfo": [ { "_id": 2, "country": "UK" } ]
  },
  {
    "_id": 2,
    "price": 101,
    "shop": 2,
    "shopInfo": { "_id": 2, "user": "1" },
    "userInfo": [ { "_id": 1, "country": "US" } ]
  }
]

内容的提问来源于stack exchange,提问作者Dashiell Rose Bark-Huss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 04:10:26