2 / 2
Jul 2024

HI All, I’m using MongoDB in Ktor backend server.

I have a below data class

@Serializable data class Teachers( @SerialName("_id") @Serializable(with = ObjectIdAsStringSerializer::class) val id: ObjectId = ObjectId(), val teacherId: String, val salutation: String, val firstName: String, val lastName: String, val address: Address, val email: String, val dob: String, val gender: String, val nationality: String, val phone: String, val subject: HashMap<String, String>, val profileImage: String, val timetable: List<TimetableEntry> = emptyList() // List to track the teacher's timetable ) @Serializable data class TimetableEntry( val day: String, // e.g., "Monday" val startTime: String, // "08:30" val endTime: String, // "10:00" ) schoolTeacherDb.updateOne( Filters.eq("teacherId", teacherId), Updates.addToSet("timetable", TimetableEntry( day = day, startTime = startTime, endTime = endTime, )) ).wasAcknowledged()

This is run thorugh an api which return 200 status , there are no error in mongodb logs but the document is not getting updated

I also tried Push and set methods, but still the outcome. There is no error api also returns 200 . but document does not get update in the DB.

Adding the code for the route which makes the api call

post("v1/teacher/update/timetable") { val teacherId = call.parameters["teacherId"] val update = call.receive<TimetableEntryUpdate>() if (teacherId == null) { call.respond(HttpStatusCode.BadRequest, "Teacher ID is required") return@post } try { val teacherUpdated = schoolTeacherDb?.updateTeacherTimeTable( teacherId = teacherId, day = update.day, startTime = update.startTime, endTime = update.endTime ) if (teacherUpdated == true) { call.respond(HttpStatusCode.OK, SimpleResponse(true, "Timetable Updated for the teacher")) } else { call.respond(HttpStatusCode.OK, SimpleResponse(false,"Timetable Not Updated")) } } catch (e: Exception) { call.respond(HttpStatusCode.Conflict,e.printStackTrace()?:"Error, please contact support") } }