Skip to main content
Code Review

Return to Question

Notice removed Draw attention by Community Bot
Bounty Ended with no winning answer by Community Bot
Notice added Draw attention by AdamK
Bounty Started worth 50 reputation by AdamK
edited title
Link
AdamK
  • 35
  • 5

Shortening a Cleaning Kotlin method which returns the document name after the id

Source Link
AdamK
  • 35
  • 5

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

AltStyle によって変換されたページ (->オリジナル) /