CollectionReference

class CollectionReference : Query


A CollectionReference can be used for adding documents, getting document references, and querying for documents (using the methods inherited from Query).

Subclassing Note: Cloud Firestore classes are not meant to be subclassed except for use in test mocks. Subclassing is not supported in production code and new SDK releases may break code that does so.

Summary

Public functions

Task<DocumentReference!>
add(data: Any)

Adds a new document to this collection with the specified data, assigning it a document ID automatically.

DocumentReference

Returns a DocumentReference pointing to a new document with an auto-generated ID within this collection.

DocumentReference
document(documentPath: String)

Gets a DocumentReference instance that refers to the document at the specified path within this collection.

String
DocumentReference?

Gets a DocumentReference to the document that contains this collection.

String

Gets the path of this collection (relative to the root of the database) as a slash-separated string.

Inherited functions

From com.google.firebase.firestore.Query
ListenerRegistration

Starts listening to this query.

ListenerRegistration
addSnapshotListener(
    activity: Activity,
    listener: EventListener<QuerySnapshot!>
)

Starts listening to this query using an Activity-scoped listener.

ListenerRegistration
addSnapshotListener(
    executor: Executor,
    listener: EventListener<QuerySnapshot!>
)

Starts listening to this query.

ListenerRegistration
addSnapshotListener(
    metadataChanges: MetadataChanges,
    listener: EventListener<QuerySnapshot!>
)

Starts listening to this query with the given options.

ListenerRegistration

Starts listening to this query with the given options.

ListenerRegistration
addSnapshotListener(
    activity: Activity,
    metadataChanges: MetadataChanges,
    listener: EventListener<QuerySnapshot!>
)

Starts listening to this query with the given options, using an Activity-scoped listener.

ListenerRegistration
addSnapshotListener(
    executor: Executor,
    metadataChanges: MetadataChanges,
    listener: EventListener<QuerySnapshot!>
)

Starts listening to this query with the given options.

AggregateQuery
aggregate(
    aggregateField: AggregateField,
    aggregateFields: Array<AggregateField!>
)

Calculates the specified aggregations over the documents in the result set of the given query without actually downloading the documents.

AggregateQuery

Returns a query that counts the documents in the result set of this query.

Query
endAt(fieldValues: Array<Any!>!)

Creates and returns a new Query that ends at the provided fields relative to the order of the query.

Query

Creates and returns a new Query that ends at the provided document (inclusive).

Query
endBefore(fieldValues: Array<Any!>!)

Creates and returns a new Query that ends before the provided fields relative to the order of the query.

Query

Creates and returns a new Query that ends before the provided document (exclusive).

Boolean
equals(o: Any!)
Task<QuerySnapshot!>
get()

Executes the query and returns the results as a QuerySnapshot.

Task<QuerySnapshot!>
get(source: Source)

Executes the query and returns the results as a QuerySnapshot.

FirebaseFirestore

Gets the Cloud Firestore instance associated with this query.

Int
Query
limit(limit: Long)

Creates and returns a new Query that only returns the first matching documents up to the specified number.

Query
limitToLast(limit: Long)

Creates and returns a new Query that only returns the last matching documents up to the specified number.

Query
orderBy(field: String)

Creates and returns a new Query that's additionally sorted by the specified field.

Query
orderBy(fieldPath: FieldPath)

Creates and returns a new Query that's additionally sorted by the specified field.

Query
orderBy(field: String, direction: Query.Direction)

Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.

Query
orderBy(fieldPath: FieldPath, direction: Query.Direction)

Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.

Query
startAfter(fieldValues: Array<Any!>!)

Creates and returns a new Query that starts after the provided fields relative to the order of the query.

Query

Creates and returns a new Query that starts after the provided document (exclusive).

Query
startAt(fieldValues: Array<Any!>!)

Creates and returns a new Query that starts at the provided fields relative to the order of the query.

Query

Creates and returns a new Query that starts at the provided document (inclusive).

Query
where(filter: Filter)

Creates and returns a new Query with the additional filter.

Query
whereArrayContains(field: String, value: Any)

Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain the provided value.

Query
whereArrayContains(fieldPath: FieldPath, value: Any)

Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain the provided value.

Query

Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain at least one value from the provided list.

Query
whereArrayContainsAny(fieldPath: FieldPath, values: (Mutable)List<Any!>)

Creates and returns a new Query with the additional filter that documents must contain the specified field, the value must be an array, and that the array must contain at least one value from the provided list.

Query
whereEqualTo(field: String, value: Any?)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be equal to the specified value.

Query
whereEqualTo(fieldPath: FieldPath, value: Any?)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be equal to the specified value.

Query
whereGreaterThan(field: String, value: Any)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than the specified value.

Query
whereGreaterThan(fieldPath: FieldPath, value: Any)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than the specified value.

Query

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than or equal to the specified value.

Query
whereGreaterThanOrEqualTo(fieldPath: FieldPath, value: Any)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be greater than or equal to the specified value.

Query
whereIn(field: String, values: (Mutable)List<Any!>)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value must equal one of the values from the provided list.

Query
whereIn(fieldPath: FieldPath, values: (Mutable)List<Any!>)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value must equal one of the values from the provided list.

Query
whereLessThan(field: String, value: Any)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less than the specified value.

Query
whereLessThan(fieldPath: FieldPath, value: Any)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less than the specified value.

Query
whereLessThanOrEqualTo(field: String, value: Any)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less than or equal to the specified value.

Query
whereLessThanOrEqualTo(fieldPath: FieldPath, value: Any)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should be less than or equal to the specified value.

Query
whereNotEqualTo(field: String, value: Any?)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal the specified value.

Query
whereNotEqualTo(fieldPath: FieldPath, value: Any?)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal the specified value.

Query
whereNotIn(field: String, values: (Mutable)List<Any!>)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal any of the values from the provided list.

Query
whereNotIn(fieldPath: FieldPath, values: (Mutable)List<Any!>)

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value does not equal any of the values from the provided list.

Public functions

add

fun add(data: Any): Task<DocumentReference!>

Adds a new document to this collection with the specified data, assigning it a document ID automatically.

Parameters
data: Any

The data to write to the document (like a Map or a POJO containing the desired document contents).

Returns
Task<DocumentReference!>

A Task that will be resolved with the DocumentReference of the newly created document.

document

fun document(): DocumentReference

Returns a DocumentReference pointing to a new document with an auto-generated ID within this collection.

Returns
DocumentReference

A DocumentReference pointing to a new document with an auto-generated ID.

document

fun document(documentPath: String): DocumentReference

Gets a DocumentReference instance that refers to the document at the specified path within this collection.

Parameters
documentPath: String

A slash-separated relative path to a document.

Returns
DocumentReference

The DocumentReference instance.

getId

fun getId(): String
Returns
String

The ID of the collection.

getParent

fun getParent(): DocumentReference?

Gets a DocumentReference to the document that contains this collection. Only subcollections are contained in a document. For root collections, returns null.

Returns
DocumentReference?

The DocumentReference that contains this collection or null if this is a root collection.

getPath

fun getPath(): String

Gets the path of this collection (relative to the root of the database) as a slash-separated string.

Returns
String

The path of this collection.