Mongoose no convierte automáticamente una cadena a ObjectId dentro del pipeline de agregación. Esa coerción automática que existe en consultas normales no está disponible aquí.
Debes convertir el valor antes de ejecutar $match:
const existedRelation = await this.model.aggregate([
{ $match: { "_id": mongoose.Types.ObjectId(firstId) } },
{ $project: {
relations: {
$filter: {
input: "$links",
as: "link",
cond: {
$and: [
{ $eq: ["$$link.target.entityId", objectIdValue] },
{ $eq: ["$$link.linkTypeId", linkTypeId] }
]
}
}
}
} }
])Envolver el identificador con mongoose.Types.ObjectId(...) permite que el motor vea un ObjectId real y la coincidencia funcione.