Filter results using $match in MongoDB aggregate returning blank array

Solution for Filter results using $match in MongoDB aggregate returning blank array
is Given Below:

I have the following schema:

const UserQualificationSchema = new Schema(
    {
        user: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User',
        },
        qualification: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Qualification',
        },
        expiry_date: {
            type: Date
        }
    }
const QualificationSchema = new Schema(
    {
        fleet: {
            type: [String], // Eg ["FleetA", "FleetB", "FleetC"]
            required: true,
        }
    }

I am searching the UserQualifications with filters in a table, to search them by fleet, qualification or expiry date. I so far have the following aggregate:

db.UserQualifications.aggregate([{
{
    $lookup: {
      from: 'qualifications',
      localField: 'qualification',
      foreignField: '_id',
      as: 'qualification',
    },
},
{
  $unwind: '$qualification',
},
{
  $match: {
    $and: [
      'qualification.fleet': {
         $in: ["Fleet A", "Fleet C"],  // This works
       },
      expiry_date: {
         $lt: req.body.expiry_date,  // This works
       },
      qualification: { // Also tried 'qualification._id'
         $in: ["6033e4129070031c07fbbf29"] // Adding this returns blank array
      }
    ]
  },
}
}])

Filtering by fleet, and expiry date both work, independently and in combination, however when adding by the qualification ID, it returns blank despite the ID’s being sent in being valid.

Am i missing something here?

Looking at your schema I can infer that qualification in ObjectId and in the query you are passing only the string value of ObjectId. You can pass the ObjectId to get your expected output

db.UserQualifications.aggregate([
  {
    $lookup: {
      from: "Qualifications",
      localField: "qualification",
      foreignField: "_id",
      as: "qualification",
      
    },
    
  },
  {
    $unwind: "$qualification",
    
  },
  {
    $match: {
      "qualification.fleet": {
        $in: [
          "FleetA",
          "FleetC"
        ],
        
      },
      expiry_date: {
        $lt: 30 // some dummy value to make it work
      },
      "qualification._id": {
        $in: [
         // some dummy value to make it work
          ObjectId("5a934e000102030405000000") 
        ]
      }
    },
    
  }
])

I have created a playground with some dummy data to test the query: Mongo Playground

Also, In $match stage there is no need to combine query explicitly in $and as by default behaviour will be same as $and only so I have remove that part in my query