Filter Query using KMongo

276 Views Asked by At

I want to filter out the courses on the basis of match query to the courseTitle.

So How can I perform this operation via **KMONGO **at server side ?

This is my Course Data Class

And I have a course document in MongoDB compass.

@Serializable

data class Course(

    @BsonId

    var id: String = ObjectId().toString(),

    val courseTitle: String,

    val imageUrl: String,

    val description: String,

    val noOfStudentEnrolled: Int,

    val course_teacher : Teacher,

    var noOfLessons: Int,

    val noOfStudentRated: Int,

    val rating: Double,

    val tag: String? = null,

    val lessons: List<Lesson>,

    val time: String,

)

I have tried using following code by referring KMONGO website. https://litote.org/KMongo/typed-queries/

Val courses = database.getCollection() Val filtercourse = courses.aggregate(match(Course:: courseTitle contains "query" )).

Here contains function can only be applied on array.

1

There are 1 best solutions below

0
On

It seems that 'contains' is used to filter arrays. Maybe using RegEx is a better solution like Course::courseTitle.regex("query", "i")