Class CollectionReference (3.21.1)

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

Inheritance

Object > Query > CollectionReference

Methods

add(Object pojo)

public ApiFuture<DocumentReference> add(Object pojo)

Adds a new document to this collection with the specified POJO as contents, assigning it a document ID automatically. See Also: #document()

Parameter
Name Description
pojo Object

The POJO that will be used to populate the contents of the document

Returns
Type Description
ApiFuture<DocumentReference>

An ApiFuture that will be resolved with the DocumentReference of the newly created document.

add(Map<String,Object> fields)

public ApiFuture<DocumentReference> add(Map<String,Object> fields)

Adds a new document to this collection with the specified data, assigning it a document ID automatically. See Also: #document()

Parameter
Name Description
fields Map<String,Object>

A Map containing the data for the new document.

Returns
Type Description
ApiFuture<DocumentReference>

An ApiFuture that will be resolved with the DocumentReference of the newly created document.

document()

public DocumentReference document()

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

Returns
Type Description
DocumentReference

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

document(String childPath)

public DocumentReference document(String childPath)

Gets a DocumentReference instance that refers to the document that is a child of this Collection.

Parameter
Name Description
childPath String

A relative and slash-separated path to a document.

Returns
Type Description
DocumentReference

The DocumentReference instance.

getId()

public String getId()

The id of a collection refers to the last component of path pointing to a collection, for example "collection-id" in "projects/project-id/databases/database-id/documents/collection-id".

Returns
Type Description
String

The ID of the collection.

getParent()

public DocumentReference getParent()

Returns a DocumentReference to the containing Document if this is a subcollection, else null.

Returns
Type Description
DocumentReference

A DocumentReference pointing to the parent document.

getPath()

public String getPath()

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

Returns
Type Description
String

The slash-separated path relative to the root of the database.

listDocuments()

public Iterable<DocumentReference> listDocuments()

Retrieves the list of documents in this collection.

The document references returned may include references to "missing documents", specifically document locations that have no document present but which contain subcollections with documents. Attempting to read such a document reference (for example via get() or onSnapshot()) will return a DocumentSnapshot whose exists() method returns false.

Returns
Type Description
Iterable<DocumentReference>

The list of documents in this collection.