Docs

Swift: Documents

Read, write and query document collections, count and aggregate, and commit several writes at once with the Swift SDK.

Updated Oct 24, 2026

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([...]).

public final class SovariaCollections: @unchecked Sendable

SovariaCollections.collection

public func collection(_ path: String) -> CollectionRef

SovariaCollections.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.

public func collectionGroup(_ collectionId: String) -> CollectionRef

Document cursors and __key__ are full document paths in a group.

SovariaCollections.commit

Write several documents atomically (["op": "set"|"update"|"delete", "path": "c/id", "data": [...]]).

public func commit(_ writes: [[String: Any]], reads: [[String: Any]] = []) async throws -> [String: Any]

CollectionRef

public struct CollectionRef: @unchecked Sendable

CollectionRef.path

A property.

public let path: String

CollectionRef.doc

public func doc(_ id: String) -> DocumentRef

CollectionRef.add

Add a document with a new random id; returns its reference.

public func add(_ data: [String: Any]) async throws -> DocumentRef

CollectionRef.query

Field query: query(where: [["age", ">=", 18]], orderBy: [["age", "desc"]], limit: 20).

public func query(where filters: [Any] = [], orderBy: [[Any]] = [], limit: Int? = nil,
                  startAfter: [String: Any]? = nil) async throws -> QueryPage

A where entry can be a group: QueryFilter.or([["a", "==", 1], ["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.

public func findNearest(_ vectorField: String, queryVector: [Double], limit: Int, distanceMeasure: String = "",
                        where filters: [Any] = [], distanceThreshold: Double? = nil,
                        distanceResultField: String? = nil) async throws -> QueryPage

distanceMeasure: "cosine" | "euclidean" | "dot_product". distanceThreshold: at most this distance (cosine/euclidean) or at least this product (dot_product).

CollectionRef.count

public func count(where filters: [Any] = []) async throws -> Int

CollectionRef.aggregate

count / sum / average on the server: aggregate(["n": Aggregate.count(), "revenue": Aggregate.sum("total")], where: [["paid", "==", true]]).

public func aggregate(_ aggregations: [String: [String: Any]], where filters: [Any] = [], orderBy: [[Any]] = [],
                      limit: Int? = nil) async throws -> [String: Double?]

sum and average use numeric values only; average is nil when there are none. With limit they cover the first limit results (in orderBy order); without, every match.

DocumentRef

public struct DocumentRef: @unchecked Sendable

DocumentRef.collection

A property.

public let collection: String

DocumentRef.id

A property.

public let id: String

DocumentRef.get

The document's data, or nil when it doesn't exist.

public func get() async throws -> [String: Any]?

DocumentRef.set

public func set(_ data: [String: Any]) async throws

DocumentRef.update

Merge fields into the document (others are kept).

public func update(_ fields: [String: Any]) async throws

DocumentRef.delete

public func delete() async throws

QueryPage

public struct QueryPage: @unchecked Sendable

QueryPage.documents

A property.

public let documents: [[String: Any]]

QueryPage.hasMore

A property.

public let hasMore: Bool

QueryPage.nextCursor

A property.

public let nextCursor: [String: Any]?

QueryFilter

or / and groups for where (Firestore Filter.orFilter / Filter.andFilter; at most 30 OR terms after expansion).

public enum QueryFilter

QueryFilter.or

public static func or(_ filters: [Any]) -> [String: Any]

QueryFilter.and

public static func and(_ filters: [Any]) -> [String: Any]

Aggregate

Aggregations for CollectionRef.aggregate.

public enum Aggregate

Aggregate.count

public static func count() -> [String: Any]

Aggregate.sum

public static func sum(_ field: String) -> [String: Any]

Aggregate.average

public static func average(_ field: String) -> [String: Any]