$lookup in aggregate but array is empty

28 Views Asked by At

I used $lookup to join a connection. But it takes empty list in joining

``` var userId="65421d801c128f2ed899a7a4";``

aggregate([

   {
     $unwind: {
        path: "$reserve",
        preserveNullAndEmptyArrays: true
    }
   },
{
  $lookup: {
    from: "reserves",
    let: {
      reserveData: "$reserve"
    },
    pipeline: [{
      $match: {
        $expr: {
          // $eq: ["$$reserveData", "$_id"],  //return  empty list
          
          // $eq: [new mongoose.Types.ObjectId(userId),"$_id"],// This code works
 
          $eq: [new mongoose.Types.ObjectId("$$reserveData"),"$_id"],// This code error

        }
      },
    }, ],
    as: "reserve"
  }
    ])

and the result when use userId

{
    "status": "success",
    "message": "okkkk",
    "results": 1,
    "data": [
        {
            "_id": "sdfgsd56",
            "customer": "fghdfbv",
            "price": 170000,
            "off": 30000,

            "reserve": [
                {
                    "_id": "65421d801c128f2ed899a7a4",
                    "customer": "6540debf6ca6663eb886eb46",
                    "service": "65421c961c128f2ed899a631",
                    "__v": 0
                }
            ]
        }
    ]
}

but use $eq: [new mongoose.Types.ObjectId("$$reserveData"),"$_id"] get error : "Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer" and error whene use
{$toObjectId: "$$reserveData"} {$toString: "$$reserveData"}

0

There are 0 best solutions below