'Mongodb aggregate - using $and and $eq inside a $match operator

I wanted to match and get the documents I wanted based on 2 conditions data

But it seems like the $eq inside $match isn't doing the right job, I checked the docs many times but can't find what exactly is the problem.

[
   {
      "episode_title":"Episode Title 1",
      "episode_number":"01",
      "unique_id":"5D66EEDA9BF055BE80CB997EFD571636C02ED050",
      "series_id":1,
      "comments_hash":"CBBEA21FE1E4",
      "quality":1080,
      "thumbnail_url":null,
      "thumbnail_version":null,
      "original_thumbnail":null,
      "watched_by":[
         1820242622
      ]
   },
   {
      "episode_title":"Episode Title 2",
      "episode_number":"02",
      "unique_id":"12DC0F2B6D1223D8FF6C10189B6CF6DEB4BBA60B",
      "series_id":1,
      "comments_hash":"EEDA9BF055BE80C",
      "quality":720,
      "thumbnail_url":null,
      "thumbnail_version":null,
      "original_thumbnail":null,
      "watched_by":[
         1820242622
      ]
   },
   {
      "episode_title":"Episode Title 1",
      "episode_number":"01",
      "unique_id":"635F12999D0641C95CBBEA21FE1E46BF26BDCCCA",
      "series_id":2,
      "comments_hash":"7EFD571636",
      "quality":1080,
      "thumbnail_url":null,
      "thumbnail_version":null,
      "original_thumbnail":null,
      "watched_by":[
         1820242622
      ]
   }
]

The data in USERS collection looks something like this:

[
   {
      "user_id":1820242622,
      "full_name":"user 1",
      "start_date":"2021-11-18T19:36:27.695953",
      "is_banned":false,
      "preferred_quality":1080
   },
   {
      "user_id":1820242624,
      "full_name":"user 2",
      "start_date":"2021-11-18T19:36:27.695953",
      "is_banned":false,
      "preferred_quality":0
   }
]

and I used this code (pymongo):

user_id = 1820242622
series_id = 1

db.data_col.aggregate([
   {
      "$lookup":{
         "from":"USERS",
         "pipeline":[
            {
               "$match":{
                  "user_id":"user_id"
               }
            },
            {
               "$project":{
                  "preferred_quality":1
               }
            }
         ],
         "as":"preferred_quality"
      }
   },
   {
      "$match":{
         "$and":[
            {
               "$eq":[
                  "$series_id",
                  "series_id"
               ]
            },
            {
               "$in":[
                  "$quality",
                  [
                     0,
                     "$preferred_quality"
                  ]
               ]
            }
         ]
      }
   },
   {
      "$set":{
         "preferred_quality":"$preferred_quality"
      }
   },
   {
      "$set":{
         "user_watched":{
            "$in":[
               "user_id",
               "$watched_by"
            ]
         }
      }
   },
   {
      "$set":{
         "watched_by":"$$REMOVE"
      }
   }
])

but I'm getting:

raise OperationFailure(errmsg, code, response, max_wire_version) pymongo.errors.OperationFailure: unknown top level operator: $eq. If you have a field name that starts with a '$' symbol, consider using $getField or $setField., full error: {'ok': 0.0, 'errmsg': "unknown top level operator: $eq. If you have a field name that starts with a '$' symbol, consider using $getField or $setField.", 'code': 2, 'codeName': 'BadValue', '$clusterTime': {'clusterTime': Timestamp(1648755167, 3), 'signature': {'hash': b"\x81\xd59\xae\x04'\x15\xac0\x08X6\x86\xc8\xa4\xf22\x07\x87\xd2", 'keyId': 7037880200322875408}}, 'operationTime': Timestamp(1648755167, 3)}

I was expecting something like this:

{
   "episode_title":"Episode Title 1",
   "episode_number":"01",
   "unique_id":"635F12999D0641C95CBBEA21FE1E46BF26BDCCCA",
   "series_id":1,
   "comments_hash":"7EFD571636",
   "quality":1080,
   "thumbnail_url":null,
   "thumbnail_version":null,
   "original_thumbnail":null,
   "preferred_quality":1080,
   "user_watched":false
}


Solution 1:[1]

db.data_col.aggregate([
  {
    $match: {
      "unique_id": "635F12999D0641C95CBBEA21FE1E46BF26BDCCCA"
    }
  },
  {
    $lookup: {
      from: "USERS",
      localField: "watched_by",
      foreignField: "user_id",
      as: "preferred_quality"
    }
  },
  {
    $set: {
      preferred_quality: {
        $first: "$preferred_quality.preferred_quality"
      }
    }
  }
])

mongoplayground

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 YuTing