Class CollectionReference


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

  • Method Details

    • getId

      @Nonnull 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:
      The ID of the collection.
    • getParent

      @Nullable public DocumentReference getParent()
      Returns a DocumentReference to the containing Document if this is a subcollection, else null.
      Returns:
      A DocumentReference pointing to the parent document.
    • getPath

      @Nonnull public String getPath()
      A string representing the path of the referenced collection (relative to the root of the database).
      Returns:
      The slash-separated path relative to the root of the database.
    • document

      @Nonnull public DocumentReference document()
      Returns a DocumentReference pointing to a new document with an auto-generated ID within this collection.
      Returns:
      A DocumentReference pointing to a new document with an auto-generated ID.
    • document

      @Nonnull public DocumentReference document(@Nonnull String childPath)
      Gets a DocumentReference instance that refers to the document that is a child of this Collection.
      Parameters:
      childPath - A relative and slash-separated path to a document.
      Returns:
      The DocumentReference instance.
    • listDocuments

      @Nonnull 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:
      The list of documents in this collection.
    • add

      @Nonnull public com.google.api.core.ApiFuture<DocumentReference> add(@Nonnull Map<String,Object> fields)
      Adds a new document to this collection with the specified data, assigning it a document ID automatically.
      Parameters:
      fields - A Map containing the data for the new document.
      Returns:
      An ApiFuture that will be resolved with the DocumentReference of the newly created document.
      See Also:
    • add

      public com.google.api.core.ApiFuture<DocumentReference> add(Object pojo)
      Adds a new document to this collection with the specified POJO as contents, assigning it a document ID automatically.
      Parameters:
      pojo - The POJO that will be used to populate the contents of the document
      Returns:
      An ApiFuture that will be resolved with the DocumentReference of the newly created document.
      See Also: