'$elemMatch in array object with $in and $exist

  [{
    _id: 6231cbafee8a3b08abb4ae67,
    jadwal: {
      _id: 6231cb8bbf823408b62f7280,
      paket: [
             paket : 6225ae9ac883840368c290cb
             show_total_question:10
             token:"TPKTGO"
             },
             {
             paket : 616ee502a022be00116121c5
             show_total_question:10
             token:null
             }],
    },
    paket: {
      _id: 6225ae9ac883840368c290cb,
    }
  },
  {
    _id: 616ef62ca022be00116121df,
    jadwal: {
      _id: 616eee195e09f600183a267b,
      paket: [
             paket : 616ee3f32a30160019829bc1
             show_total_question:10
             },
             {
             paket : 616f12c7d2a514003018831f
             show_total_question:10
             token:"KQABIN"
             }],
    },
    paket: {
      _id: 616ee3f32a30160019829bc1,
    }
  },
]

First i have those two sample data from aggregation $lookup reference to jadwal and paket. i have to filter the data like matching paket._id with jadwal.paket.paket and then tried to check if the token is not exist or have value [null,""]. Soo the output that i hope is

 {
        _id: 616ef62ca022be00116121df,
        jadwal: {
          _id: 616eee195e09f600183a267b,
          paket: [
                 paket : 616ee3f32a30160019829bc1
                 show_total_question:10
                 },
                 {
                 paket : 616f12c7d2a514003018831f
                 show_total_question:10
                 token:"KQABIN"
                 }],
        },
        paket: {
          _id: 616ee3f32a30160019829bc1,
        }
   }

Because in paket._id have token.value in jadwal.paket.token is not exists. The condition on this data is paket._id is always have copy or same value in jadwal.paket.paket i use this code in bellow of my aggregation

{'jadwal.paket':{'$elemMatch':{'paket':'$pakets._id','token':{$exists:false}

but seems not working, or perhaps i do it wrong? please help me



Solution 1:[1]

I got the answer first you have to unwind the jadwal.paket and then use $expr as a filter

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ghozi Run