如何在猫鼬$aggregate>$lookup->管道>$match中访问字段名称



我的代码以下。

Users.aggregate([{
     $lookup: {
          from: "payments",
          // localField:"_id",
          // foreignField:"restaurant_id",
          let: {
               id: "$_id"
          },
          pipeline: [
               {
                    $match: {
                         status: "Active", // If I add only this line it returns the result ->
                         restaurant_id:"$id" //or "$_id"  ->but after adding this line it return empty array
                         //if I change $id to static value of id result comes 
                    },
               }
          ],
          as: "subscription"
     }
}])

我无法在$match中使用字段名称。如果我编写静态ID,则工作正常,但是我需要使用动态_id。请建议如何在$match中使用字段名称。我使用了许多诸如$and $expr之类的东西,但是什么都没有。

两个集合在同一db

mongoDB服务器版本:3.6.9

" mongoose":"^5.3.4"

我已经从此参考中解决了问题,以$查找

指定多个加入条件
Users.aggregate([
     {
          $lookup: {
               from: "payments",
               // localField:"_id",
               // foreignField:"restaurant_id",
               let: {
                    id: "$_id" //localField
               },
               pipeline: [
                    {
                         $match: {
                              $expr:{
                                   $and:[
                                        {$eq: ["$status","Active"]},
                                        {
                                             $eq:[
                                                  "$$id", //localField variable it can be used only in $expr
                                                  "$restaurant_id" //foreignField 
                                             ]
                                        }
                                   ]
                              }

                         },
                    }
               ],
               as: "subscription"
          }
     }
])

最新更新