DocumentSnapshot

public class DocumentSnapshot extends Object
Known Direct Subclasses

A DocumentSnapshot contains data read from a document in your Firestore database. The data can be extracted with the getData() or get() 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.

Nested Class Summary

enum DocumentSnapshot.ServerTimestampBehavior Controls the return value for server timestamps that have not yet been set to their final value. 

Public Method Summary

boolean
contains(FieldPath fieldPath)
Returns whether or not the field exists in the document.
boolean
contains(String field)
Returns whether or not the field exists in the document.
boolean
boolean
Object
get(String field, DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior)
Returns the value at the field or null if the field doesn't exist.
Object
get(FieldPath fieldPath)
Returns the value at the field or null if the field or document doesn't exist.
Object
get(FieldPath fieldPath, DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior)
Returns the value at the field or null if the field or document doesn't exist.
Object
get(String field)
Returns the value at the field or null if the field doesn't exist.
Blob
getBlob(String field)
Returns the value of the field as a Blob.
Boolean
getBoolean(String field)
Returns the value of the field as a boolean.
Map<StringObject>
getData(DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior)
Returns the fields of the document as a Map or null if the document doesn't exist.
Map<StringObject>
getData()
Returns the fields of the document as a Map or null if the document doesn't exist.
Date
getDate(String field)
Returns the value of the field as a Date.
Date
getDate(String field, DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior)
Returns the value of the field as a Date.
DocumentReference
getDocumentReference(String field)
Returns the value of the field as a DocumentReference.
Double
getDouble(String field)
Returns the value of the field as a double.
GeoPoint
getGeoPoint(String field)
Returns the value of the field as a GeoPoint.
String
Long
getLong(String field)
Returns the value of the field as a long.
SnapshotMetadata
DocumentReference
getReference()
Gets the reference to the document.
String
getString(String field)
Returns the value of the field as a String.
Timestamp
getTimestamp(String field, DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior)
Returns the value of the field as a Timestamp.
Timestamp
getTimestamp(String field)
Returns the value of the field as a Timestamp.
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.
<T> T
toObject(Class<T> valueType, DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior)
Returns the contents of the document converted to a POJO or null if the document doesn't exist.
String

Inherited Method Summary

Public Methods

public boolean contains (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.

public boolean contains (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.

public boolean equals (Object obj)

public boolean exists ()

Returns
  • true if the document existed in this snapshot.

public Object get (String field, DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior)

Returns the value at the field or null if the field doesn't exist.

Parameters
field The path to the field
serverTimestampBehavior Configures the behavior for server timestamps that have not yet been set to their final value.
Returns
  • The value at the given field or null.

public Object get (FieldPath fieldPath)

Returns the value at the field or null if the field or document doesn't exist.

Parameters
fieldPath The path to the field
Returns
  • The value at the given field or null.

public Object get (FieldPath fieldPath, DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior)

Returns the value at the field or null if the field or document doesn't exist.

Parameters
fieldPath The path to the field
serverTimestampBehavior Configures the behavior for server timestamps that have not yet been set to their final value.
Returns
  • The value at the given field or null.

public Object get (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.

public Blob getBlob (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.

public Boolean getBoolean (String field)

Returns the value of the field as a boolean. If the value is not a boolean this will throw a runtime exception.

Parameters
field The path to the field.
Returns
  • The value of the field

public Map<StringObject> getData (DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior)

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.

Parameters
serverTimestampBehavior Configures the behavior for server timestamps that have not yet been set to their final value.
Returns
  • The fields of the document as a Map or null if the document doesn't exist.

public Map<StringObject> 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.

public Date getDate (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.

public Date getDate (String field, DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior)

Returns the value of the field as a Date.

This method ignores the global setting areTimestampsInSnapshotsEnabled().

Parameters
field The path to the field.
serverTimestampBehavior Configures the behavior for server timestamps that have not yet been set to their final value.
Returns
  • The value of the field
Throws
RuntimeException if the value is not a Date.

public DocumentReference getDocumentReference (String field)

Returns the value of the field as a DocumentReference.

Parameters
field The path to the field.
Returns
  • The value of the field
Throws
RuntimeException if the value is not a DocumentReference.

public Double getDouble (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.

public GeoPoint getGeoPoint (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.

public String getId ()

Returns
  • The id of the document.

public Long getLong (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.

public SnapshotMetadata getMetadata ()

Returns
  • The metadata for this document snapshot.

public DocumentReference getReference ()

Gets the reference to the document.

Returns
  • The reference to the document.

public String getString (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.

public Timestamp getTimestamp (String field, DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior)

Returns the value of the field as a Timestamp.

This method ignores the global setting areTimestampsInSnapshotsEnabled().

Parameters
field The path to the field.
serverTimestampBehavior Configures the behavior for server timestamps that have not yet been set to their final value.
Returns
  • The value of the field
Throws
RuntimeException if the value is not a timestamp field.

public Timestamp getTimestamp (String field)

Returns the value of the field as a Timestamp.

This method ignores the global setting areTimestampsInSnapshotsEnabled().

Parameters
field The path to the field.
Returns
  • The value of the field
Throws
RuntimeException if this is not a timestamp field.

public int hashCode ()

public T toObject (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.

public T toObject (Class<T> valueType, DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior)

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
serverTimestampBehavior Configures the behavior for server timestamps that have not yet been set to their final value.
Returns
  • The contents of the document in an object of type T or null if the document doesn't exist.

public String toString ()