Shortening a Cleaning Kotlin method which returns the document name after the id
Shortening a method which returns the document name after the id
I'm just getting to know the possibilities of Kotlin and mongoDB.
I am writing a method that returns the name of the street after the ID.
Everything works, but I find it quite sloppy.
Empty String initialization, returning more data from the collection than I would like.
How do I straighten it? Could it be made into some expression body one-liner?
fun getStreetNameByStreetId(id: String): String {
val query = Query()
query.fields().include("name")
query.addCriteria(Criteria.where("_id").`is`(id))
var streetName = ""
mongoTemplate.executeQuery(
query, STREET_COLLECTION
) { document: Document ->
streetName = document.getValue("name").toString()
}
return streetName
}
default