valueAs

inline fun <T> valueAs(name: String = "$", serializer: JsonSerializer? = null): T

Returns a field value from a result with exactly one row. Useful for getting the result of an aggregating function.

Samples

import com.couchbase.client.kotlin.Cluster
import com.couchbase.client.kotlin.query.QueryMetadata
import com.couchbase.client.kotlin.query.QueryResult
import com.couchbase.client.kotlin.query.execute
fun main() { 
   //sampleStart 
   // Single-value query with anonymous result
val count = cluster
    .query("select count(*) from `travel-sample`")
    .execute()
    .valueAs<Long>() // uses default name "$1" 
   //sampleEnd
}
import com.couchbase.client.kotlin.Cluster
import com.couchbase.client.kotlin.query.QueryMetadata
import com.couchbase.client.kotlin.query.QueryResult
import com.couchbase.client.kotlin.query.execute
fun main() { 
   //sampleStart 
   // Single-value query with named result
val count = cluster
    .query("select count(*) as count from `travel-sample`")
    .execute()
    .valueAs<Long>("count") 
   //sampleEnd
}

Parameters

name

the name of the field to extract.

serializer

for converting the field value to the requested type. Defaults to the QueryRow's serializer.

T

the serializer reads the field value as this type.

Throws

if there is more than one result row

if there is no field with the given name