Jump to top

CollectionReference

interface

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

Properties

id

</>

The collection's identifier.

id: string;

parent

</>

A reference to the containing DocumentReference if this is a subcollection. If this isn't a subcollection, the reference is null.

parent: DocumentReference | null;

path

</>

A string representing the path of the referenced collection (relative to the root of the database).

path: string;

Methods

add

</>

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

add(data: { [key: string]: value }): Promise<DocumentReference>;

doc

</>

Get a DocumentReference for the document within the collection at the specified path. If no path is specified, an automatically-generated unique ID will be used for the returned DocumentReference.

doc(documentPath?: undefined | string): DocumentReference;

endAt

</>
Signature 1
</>

Creates and returns a new Query that ends at the provided document (inclusive). The end position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.

endAt(snapshot: DocumentSnapshot): Query;
Signature 2
</>

Creates and returns a new Query that ends at the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.

endAt(fieldValues: any[]): Query;

endBefore

</>
Signature 1
</>

Creates and returns a new Query that ends before the provided document (exclusive). The end position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.

endBefore(snapshot: DocumentSnapshot): Query;
Signature 2
</>

Creates and returns a new Query that ends before the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.

endBefore(fieldValues: any[]): Query;

get

</>

Executes the query and returns the results as a QuerySnapshot.

get(options?: GetOptions): Promise<QuerySnapshot>;

isEqual

</>

Returns true if this Query is equal to the provided one.

isEqual(other: Query): boolean;

limit

</>

Creates and returns a new Query where the results are limited to the specified number of documents.

limit(limit: number): Query;

limitToLast

</>

Creates and returns a new Query where the results are limited to the specified number of documents starting from the last document. The order is dependent on the second parameter for the orderBy method. If desc is used, the order is reversed. orderBy method call is required when calling limitToLast.

limitToLast(limitToLast: number): Query;

onSnapshot

</>
Signature 1
</>

Attaches a listener for QuerySnapshot events.

onSnapshot(observer: { complete: undefined | () => void, error: undefined | (error: Error) => void, next: undefined | (snapshot: QuerySnapshot) => void }): () => void;
Signature 2
</>

Attaches a listener for QuerySnapshot events with snapshot listener options.

onSnapshot(options: SnapshotListenOptions, observer: { complete: undefined | () => void, error: undefined | (error: Error) => void, next: undefined | (snapshot: QuerySnapshot) => void }): () => void;
Signature 3
</>

Attaches a listener for QuerySnapshot events.

onSnapshot(onNext: (snapshot: QuerySnapshot) => void, onError?: undefined | (error: Error) => void, onCompletion?: undefined | () => void): () => void;
Signature 4
</>

Attaches a listener for QuerySnapshot events with snapshot listener options.

onSnapshot(options: SnapshotListenOptions, onNext: (snapshot: QuerySnapshot) => void, onError?: undefined | (error: Error) => void, onCompletion?: undefined | () => void): () => void;

orderBy

</>

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

orderBy(fieldPath: string | FieldPath, directionStr?: "asc" | "desc"): Query;

startAfter

</>
Signature 1
</>

Creates and returns a new Query that starts after the provided document (exclusive). The start position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.

startAfter(snapshot: DocumentSnapshot): Query;
Signature 2
</>

Creates and returns a new Query that starts after the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.

startAfter(fieldValues: any[]): Query;

startAt

</>
Signature 1
</>

Creates and returns a new Query that starts at the provided document (inclusive). The start position is relative to the order of the query. The document must contain all of the fields provided in the orderBy of this query.

startAt(snapshot: DocumentSnapshot): Query;
Signature 2
</>

Creates and returns a new Query that starts at the provided fields relative to the order of the query. The order of the field values must match the order of the order by clauses of the query.

startAt(fieldValues: any[]): Query;

where

</>

Creates and returns a new Query with the additional filter that documents must contain the specified field and the value should satisfy the relation constraint provided.

where(fieldPath: string | FieldPath, opStr: WhereFilterOp, value: any): Query;