首页 > 解决方案 > MongoDB聚合数据

问题描述

当我从 Mongodb文档运行代码以在 $lookup 中使用管道时,
我收到一个错误,即let is not supported。如何在 $lookup 中使用 let: { <var_1>: , ..., <var_n>: }。

错误:

错误

代码:

db.orders.aggregate([
   {
      $lookup:
         {
           from: "warehouses",
           let: { order_item: "$item", order_qty: "$ordered" },
           pipeline: [
              { $match:
                 { $expr:
                    { $and:
                       [
                         { $eq: [ "$stock_item",  "$$order_item" ] },
                         { $gte: [ "$instock", "$$order_qty" ] }
                       ]
                    }
                 }
              },
              { $project: { stock_item: 0, _id: 0 } }
           ],
           as: "stockdata"
         }
    }
])

标签: mongodbaggregatelookuplookup-tables

解决方案


推荐阅读