Class CollectionReference (3.20.0)

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
NameDescription
pojoObject

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

Returns
TypeDescription
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
NameDescription
fieldsMap<String,Object>

A Map containing the data for the new document.

Returns
TypeDescription
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
TypeDescription
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
NameDescription
childPathString

A relative and slash-separated path to a document.

Returns
TypeDescription
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
TypeDescription
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
TypeDescription
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
TypeDescription
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
TypeDescription
Iterable<DocumentReference>

The list of documents in this collection.