Class DocumentSnapshot

java.lang.Object
com.google.cloud.firestore.DocumentSnapshot
Direct Known Subclasses:
QueryDocumentSnapshot

@InternalExtensionOnly public class DocumentSnapshot extends Object
A DocumentSnapshot contains data read from a document in a Firestore database. The data can be extracted with the getData() or get(String) methods.

If the DocumentSnapshot points to a non-existing document, getData() and its corresponding methods will return null. You can always explicitly check for a document's existence by calling exists().

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 Summary

    Modifier and Type
    Method
    Description
    boolean
    contains(FieldPath fieldPath)
    Returns whether or not the field exists in the document.
    boolean
    Returns whether or not the field exists in the document.
    boolean
    Returns true if the document's data and path in this DocumentSnapshot equals the provided snapshot.
    boolean
    Returns whether or not the field exists in the document.
    get(FieldPath fieldPath)
    Returns the value at the field or null if the field doesn't exist.
    <T> T
    get(FieldPath fieldPath, Class<T> valueType)
    Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.
    get(String field)
    Returns the value at the field or null if the field doesn't exist.
    <T> T
    get(String field, Class<T> valueType)
    Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.
    getBlob(String field)
    Returns the value of the field as a Blob.
    Returns the value of the field as a boolean.
    com.google.cloud.Timestamp
    Returns the time at which this document was created.
    Returns the fields of the document as a Map or null if the document doesn't exist.
    getDate(String field)
    Returns the value of the field as a Date.
    Returns the value of the field as a double.
    Returns the value of the field as a GeoPoint.
    Returns the ID of the document contained in this snapshot.
    getLong(String field)
    Returns the value of the field as a long.
    com.google.cloud.Timestamp
    Returns the time at which this snapshot was read.
    Gets the reference to the document.
    Returns the value of the field as a String.
    com.google.cloud.Timestamp
    Returns the value of the field as a Timestamp.
    com.google.cloud.Timestamp
    Returns the time at which this document was last updated.
    Returns the value of the field as a VectorValue.
    int
     
    <T> T
    toObject(Class<T> valueType)
    Returns the contents of the document converted to a POJO or null if the document doesn't exist.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • getId

      @Nonnull public String getId()
      Returns the ID of the document contained in this snapshot.
      Returns:
      The id of the document.
    • getReadTime

      @Nullable public com.google.cloud.Timestamp getReadTime()
      Returns the time at which this snapshot was read.
      Returns:
      The read time of this snapshot.
    • getUpdateTime

      @Nullable public com.google.cloud.Timestamp getUpdateTime()
      Returns the time at which this document was last updated. Returns null for non-existing documents.
      Returns:
      The last time the document in the snapshot was updated. Null if the document doesn't exist.
    • getCreateTime

      @Nullable public com.google.cloud.Timestamp getCreateTime()
      Returns the time at which this document was created. Returns null for non-existing documents.
      Returns:
      The last time the document in the snapshot was created. Null if the document doesn't exist.
    • exists

      public boolean exists()
      Returns whether or not the field exists in the document. Returns false if the document does not exist.
      Returns:
      whether the document existed in this snapshot.
    • getData

      @Nullable public Map<String,Object> getData()
      Returns the fields of the document as a Map or null if the document doesn't exist. Field values will be converted to their native Java representation.
      Returns:
      The fields of the document as a Map or null if the document doesn't exist.
    • toObject

      @Nullable public <T> T toObject(@Nonnull Class<T> valueType)
      Returns the contents of the document converted to a POJO or null if the document doesn't exist.
      Parameters:
      valueType - The Java class to create
      Returns:
      The contents of the document in an object of type T or null if the document doesn't exist.
    • contains

      public boolean contains(@Nonnull String field)
      Returns whether or not the field exists in the document. Returns false if the document does not exist.
      Parameters:
      field - the path to the field.
      Returns:
      true iff the field exists.
    • contains

      public boolean contains(@Nonnull FieldPath fieldPath)
      Returns whether or not the field exists in the document. Returns false if the document does not exist.
      Parameters:
      fieldPath - the path to the field.
      Returns:
      true iff the field exists.
    • get

      @Nullable public Object get(@Nonnull String field)
      Returns the value at the field or null if the field doesn't exist.
      Parameters:
      field - The path to the field.
      Returns:
      The value at the given field or null.
    • get

      @Nullable public <T> T get(@Nonnull String field, @Nonnull Class<T> valueType)
      Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.
      Parameters:
      field - The path to the field
      valueType - The Java class to convert the field value to.
      Returns:
      The value at the given field or null.
    • get

      @Nullable public Object get(@Nonnull FieldPath fieldPath)
      Returns the value at the field or null if the field doesn't exist.
      Parameters:
      fieldPath - The path to the field.
      Returns:
      The value at the given field or null.
    • get

      @Nullable public <T> T get(@Nonnull FieldPath fieldPath, Class<T> valueType)
      Returns the value at the field, converted to a POJO, or null if the field or document doesn't exist.
      Parameters:
      fieldPath - The path to the field
      valueType - The Java class to convert the field value to.
      Returns:
      The value at the given field or null.
    • getBoolean

      @Nullable public Boolean getBoolean(@Nonnull String field)
      Returns the value of the field as a boolean.
      Parameters:
      field - The path to the field.
      Returns:
      The value of the field.
      Throws:
      RuntimeException - if the value is not a Boolean.
    • getDouble

      @Nullable public Double getDouble(@Nonnull String field)
      Returns the value of the field as a double.
      Parameters:
      field - The path to the field.
      Returns:
      The value of the field.
      Throws:
      RuntimeException - if the value is not a Number.
    • getString

      @Nullable public String getString(@Nonnull String field)
      Returns the value of the field as a String.
      Parameters:
      field - The path to the field.
      Returns:
      The value of the field.
      Throws:
      RuntimeException - if the value is not a String.
    • getLong

      @Nullable public Long getLong(@Nonnull String field)
      Returns the value of the field as a long.
      Parameters:
      field - The path to the field.
      Returns:
      The value of the field.
      Throws:
      RuntimeException - if the value is not a Number.
    • getDate

      @Nullable public Date getDate(@Nonnull String field)
      Returns the value of the field as a Date.
      Parameters:
      field - The path to the field.
      Returns:
      The value of the field.
      Throws:
      RuntimeException - if the value is not a Date.
    • getTimestamp

      @Nullable public com.google.cloud.Timestamp getTimestamp(@Nonnull String field)
      Returns the value of the field as a Timestamp.
      Parameters:
      field - The path to the field.
      Returns:
      The value of the field.
      Throws:
      RuntimeException - if the value is not a Date.
    • getBlob

      @Nullable public Blob getBlob(@Nonnull String field)
      Returns the value of the field as a Blob.
      Parameters:
      field - The path to the field.
      Returns:
      The value of the field.
      Throws:
      RuntimeException - if the value is not a Blob.
    • getGeoPoint

      @Nullable public GeoPoint getGeoPoint(@Nonnull String field)
      Returns the value of the field as a GeoPoint.
      Parameters:
      field - The path to the field.
      Returns:
      The value of the field.
      Throws:
      RuntimeException - if the value is not a GeoPoint.
    • getVectorValue

      @Nullable public VectorValue getVectorValue(@Nonnull String field)
      Returns the value of the field as a VectorValue.
      Parameters:
      field - The path to the field.
      Returns:
      The value of the field.
      Throws:
      RuntimeException - if the value is not a VectorValue.
    • getReference

      @Nonnull public DocumentReference getReference()
      Gets the reference to the document.
      Returns:
      The reference to the document.
    • equals

      public boolean equals(Object obj)
      Returns true if the document's data and path in this DocumentSnapshot equals the provided snapshot.
      Overrides:
      equals in class Object
      Parameters:
      obj - The object to compare against.
      Returns:
      Whether this DocumentSnapshot is equal to the provided object.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object