CollectionReference

public class CollectionReference extends 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 methods

@NonNull Task<DocumentReference>

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

@NonNull DocumentReference

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

@NonNull DocumentReference
document(@NonNull String documentPath)

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

@NonNull String
@Nullable DocumentReference

Gets a DocumentReference to the document that contains this collection.

@NonNull String

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

Inherited methods

From com.google.firebase.firestore.Query
@NonNull ListenerRegistration

Starts listening to this query.

@NonNull ListenerRegistration
addSnapshotListener(
    @NonNull Activity activity,
    @NonNull EventListener<QuerySnapshot> listener
)

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

@NonNull ListenerRegistration
addSnapshotListener(
    @NonNull Executor executor,
    @NonNull EventListener<QuerySnapshot> listener
)

Starts listening to this query.

@NonNull ListenerRegistration
addSnapshotListener(
    @NonNull MetadataChanges metadataChanges,
    @NonNull EventListener<QuerySnapshot> listener
)

Starts listening to this query with the given options.

@NonNull ListenerRegistration

Starts listening to this query with the given options.

@NonNull ListenerRegistration
addSnapshotListener(
    @NonNull Activity activity,
    @NonNull MetadataChanges metadataChanges,
    @NonNull EventListener<QuerySnapshot> listener
)

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

@NonNull ListenerRegistration
addSnapshotListener(
    @NonNull Executor executor,
    @NonNull MetadataChanges metadataChanges,
    @NonNull EventListener<QuerySnapshot> listener
)

Starts listening to this query with the given options.

@NonNull AggregateQuery
aggregate(
    @NonNull AggregateField aggregateField,
    @NonNull AggregateField[] aggregateFields
)

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

@NonNull AggregateQuery

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

@NonNull Query
endAt(Object[] fieldValues)

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

@NonNull Query

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

@NonNull Query
endBefore(Object[] fieldValues)

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

@NonNull Query

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

boolean
@NonNull Task<QuerySnapshot>
get()

Executes the query and returns the results as a QuerySnapshot.

@NonNull Task<QuerySnapshot>
get(@NonNull Source source)

Executes the query and returns the results as a QuerySnapshot.

@NonNull FirebaseFirestore

Gets the Cloud Firestore instance associated with this query.

int
@NonNull Query
limit(long limit)

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

@NonNull Query
limitToLast(long limit)

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

@NonNull Query

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

@NonNull Query

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

@NonNull Query

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

@NonNull Query
orderBy(@NonNull FieldPath fieldPath, @NonNull Query.Direction direction)

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

@NonNull Query
startAfter(Object[] fieldValues)

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

@NonNull Query

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

@NonNull Query
startAt(Object[] fieldValues)

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

@NonNull Query

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

@NonNull Query
where(@NonNull Filter filter)

Creates and returns a new Query with the additional filter.

@NonNull 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 the provided value.

@NonNull 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 the provided value.

@NonNull 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.

@NonNull Query
whereArrayContainsAny(
    @NonNull FieldPath fieldPath,
    @NonNull List<Object> values
)

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.

@NonNull Query

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.

@NonNull Query

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.

@NonNull 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 the specified value.

@NonNull 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 the specified value.

@NonNull 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.

@NonNull Query
whereGreaterThanOrEqualTo(
    @NonNull FieldPath fieldPath,
    @NonNull Object value
)

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.

@NonNull Query
whereIn(@NonNull String field, @NonNull List<Object> values)

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.

@NonNull Query
whereIn(@NonNull FieldPath fieldPath, @NonNull List<Object> values)

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.

@NonNull Query

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.

@NonNull Query

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.

@NonNull Query

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.

@NonNull Query

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.

@NonNull Query

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.

@NonNull Query

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.

@NonNull Query

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.

@NonNull Query
whereNotIn(@NonNull FieldPath fieldPath, @NonNull List<Object> values)

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 methods

add

public @NonNull Task<DocumentReferenceadd(@NonNull Object data)

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

Parameters
@NonNull Object data

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

Returns
@NonNull Task<DocumentReference>

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

document

public @NonNull DocumentReference document()

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

Returns
@NonNull DocumentReference

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

document

public @NonNull DocumentReference document(@NonNull String documentPath)

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

Parameters
@NonNull String documentPath

A slash-separated relative path to a document.

Returns
@NonNull DocumentReference

The DocumentReference instance.

getId

public @NonNull String getId()
Returns
@NonNull String

The ID of the collection.

getParent

public @Nullable DocumentReference getParent()

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

Returns
@Nullable DocumentReference

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

getPath

public @NonNull String getPath()

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

Returns
@NonNull String

The path of this collection.