Dipendra Singh
09/28/2021, 8:47 PMfun 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
}
sbyrne
09/28/2021, 8:55 PMSequence<T>
, and in your case where you only want one value, do val streetName = mongoTemplate.executeQueryToSequence(query,STREET_COLLECTION) { document -> document.getValue("name").toString() }.firstOrNull()
.sbyrne
09/28/2021, 8:55 PMexecuteQueryToSequence
is the extension function I would write.)Paul Griffith
09/28/2021, 8:58 PMfindOne
already exists and is nullable, so something like this would probably be more idiomatic. As a sidenote, this function should probably return String?
since the lookup is falliblePaul Griffith
09/28/2021, 8:58 PMtodd.ginsberg
09/28/2021, 9:03 PMapply
might make things a bit cleaner:
fun getStreeNameByStreetId(id: String): String {
val query = Query().apply {
fields().include("name")
addCriteria(Criteria.where("_id").`is`(id))
}
return mongoTemplate...
}
Paul Griffith
09/28/2021, 9:05 PMPaul Griffith
09/28/2021, 9:08 PMDipendra Singh
09/28/2021, 9:12 PMPaul Griffith
09/28/2021, 10:00 PM