const p_id = patient_id; let fetchingReports = await Reports.aggregate([ ...(p_id ? [ { $match: { createdBy: mongoose.Types.ObjectId(id), patient_id: p_id, }, }, ] : [ { $match: { createdBy: mongoose.Types.ObjectId(id), }, },