Database

db={"category":[{"_id":"63d3e01f43aa4e0ee349f841","subCategories":[{"subCategoryId":NumberLong(1),"name":"Mobile phones"},{"subCategoryId":NumberLong(2),"name":"XYZ Machine"}]}],"product":[{"_id":"63d3e13b43aa4e0ee349f842","productId":NumberLong(1),"name":"iphone 14","category":{"$ref":"category","$id":"63d3e01f43aa4e0ee349f841"},"subCategoryId":NumberLong(1)}]}

Query

db.product.aggregate([{$match:{"_id":"63d3e13b43aa4e0ee349f842"}},{"$lookup":{"from":"category","let":{categoryId:"$category.$id",subCategoryId:"$subCategoryId"},"pipeline":[{$match:{$expr:{$eq:["$$categoryId","$_id"]}}},{$unwind:"$subCategories"},{$match:{$expr:{$eq:["$$subCategoryId","$subCategories.subCategoryId"]}}}],"as":"subCategoryLookup"}}])

Result