Kotlin: Documents
Read, write and query document collections, count and aggregate, and commit several writes at once with the Kotlin SDK.
Read, write and query document collections, count and aggregate, and commit several writes at once. Every call runs as the client's signed-in user, so your project's policies and row-level security apply. Types and members are listed in source order; each one links to its HTTP endpoint where it maps to one.
#Types and members
SovariaCollections
Document collections (Firestore-style): sovaria.collections.collection("chats/42/messages").add(mapOf(...)).
class SovariaCollections(private val client: Sovaria)SovariaCollections.collection
fun collection(path: String): CollectionRefSovariaCollections.collectionGroup
Every collection with this id at any depth (comments, posts/p1/comments, …), like Firestore's collectionGroup: query, count, aggregate and findNearest read them all.
fun collectionGroup(collectionId: String): CollectionRefDocument cursors and __key__ are full document paths in a group.
SovariaCollections.commit
Write several documents atomically (mapOf("op" to "set"|"update"|"delete", "path" to "c/id", "data" to ...)).
suspend fun commit(writes: List<Map<String, Any?>>, reads: List<Map<String, Any?>> = emptyList()): Map<String, Any?>CollectionRef
class CollectionRef internal constructor(private val client: Sovaria, val path: String, val isGroup: Boolean = false)CollectionRef.doc
fun doc(id: String): DocumentRefCollectionRef.add
Add a document with a new random id; returns its reference.
suspend fun add(data: Map<String, Any?>): DocumentRefCollectionRef.query
Field query: query(where = listOf(listOf("age", ">=", 18)), orderBy = listOf(listOf("age", "desc")), limit = 20).
suspend fun query(where: List<Any> = emptyList(), orderBy: List<List<Any?>> = emptyList(), limit: Int? = null,
startAfter: Map<String, Any?>? = null): QueryPageA where entry can be a group: QueryFilter.or(listOf("a", "==", 1), listOf("b", "<", 2)).
CollectionRef.findNearest
Vector search (Firestore findNearest): the limit documents whose vectorField (an array of numbers) is closest to queryVector, after the where pre-filter, closest first; each document has a distance.
suspend fun findNearest(vectorField: String, queryVector: List<Number>, limit: Int, distanceMeasure: String = "",
where: List<Any> = emptyList(), distanceThreshold: Double? = null,
distanceResultField: String? = null): QueryPagedistanceMeasure: "cosine" | "euclidean" | "dot_product". distanceThreshold: at most this distance (cosine/euclidean) or at least this product (dot_product).
CollectionRef.count
suspend fun count(where: List<Any> = emptyList()): IntCollectionRef.aggregate
count / sum / average on the server: aggregate(mapOf("n" to Aggregate.count(), "revenue" to Aggregate.sum("total")), where = listOf(listOf("paid", "==", true))).
suspend fun aggregate(aggregations: Map<String, Map<String, Any?>>, where: List<Any> = emptyList(),
orderBy: List<List<Any?>> = emptyList(), limit: Int? = null): Map<String, Number?>sum and average use numeric values only; average is null when there are none. With limit they cover the first limit results (in orderBy order); without, every match.
DocumentRef
class DocumentRef internal constructor(private val client: Sovaria, val collection: String, val id: String)DocumentRef.get
The document's data, or null when it doesn't exist.
suspend fun get(): Map<String, Any?>?DocumentRef.set
suspend fun set(data: Map<String, Any?>)DocumentRef.update
Merge fields into the document (others are kept).
suspend fun update(fields: Map<String, Any?>)DocumentRef.delete
suspend fun delete()QueryPage
class QueryPage(val documents: List<Map<String, Any?>>, val hasMore: Boolean, val nextCursor: Map<String, Any?>?)QueryPage.from
fun from(json: Any?): QueryPageQueryFilter
or / and groups for where (Firestore Filter.or / Filter.and; at most 30 OR terms after expansion).
object QueryFilterQueryFilter.or
fun or(vararg filters: Any): Map<String, Any>QueryFilter.and
fun and(vararg filters: Any): Map<String, Any>Aggregate
Aggregations for CollectionRef.aggregate.
object AggregateAggregate.count
fun count(): Map<String, Any?>Aggregate.sum
fun sum(field: String): Map<String, Any?>Aggregate.average
fun average(field: String): Map<String, Any?>