firebase:: firestore:: DocumentSnapshot
#include <document_snapshot.h>
ADocumentSnapshot contains data read from a document in yourFirestore database.
Summary
The data can be extracted with theGetData() method, or by usingGet() to access a specific field. For aDocumentSnapshot that points to a non-existing document, any data access will cause a failed assertion. You can use theexists() method to explicitly verify a document's existence.
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.
Constructors and Destructors | |
|---|---|
DocumentSnapshot()Creates an invalidDocumentSnapshot that has to be reassigned before it can be used. | |
DocumentSnapshot(constDocumentSnapshot & other)Copy constructor. | |
DocumentSnapshot(DocumentSnapshot && other)Move constructor. | |
~DocumentSnapshot() |
Public types | |
|---|---|
ServerTimestampBehavior{ | enum Controls the return value for server timestamps that have not yet been set to their final value. |
Friend classes | |
|---|---|
operator<< | friend std::ostream &Outputs the string representation of this DocumentSnapshot to the given stream. |
Public functions | |
|---|---|
Get(const char *field,ServerTimestampBehavior stb) const | virtualFieldValueRetrieves a specific field from the document. |
Get(const std::string & field,ServerTimestampBehavior stb) const | virtualFieldValueRetrieves a specific field from the document. |
Get(constFieldPath & field,ServerTimestampBehavior stb) const | virtualFieldValueRetrieves a specific field from the document. |
GetData(ServerTimestampBehavior stb) const | virtualMapFieldValueRetrieves all fields in the document as a map of FieldValues. |
ToString() const | std::stringReturns a string representation of this DocumentSnapshot for logging/debugging purposes. |
exists() const | virtual boolExplicitly verify a document's existence. |
id() const | virtual const std::string &Returns the string ID of the document for which thisDocumentSnapshot contains data. |
is_valid() const | boolReturns true if this DocumentSnapshot is valid, false if it is not valid. |
metadata() const | virtualSnapshotMetadataReturns the metadata about this snapshot concerning its source and if it has local modifications. |
operator=(constDocumentSnapshot & other) | Copy assignment operator. |
operator=(DocumentSnapshot && other) | Move assignment operator. |
reference() const | virtualDocumentReferenceReturns the document location for which thisDocumentSnapshot contains data. |
Public types
ServerTimestampBehavior
ServerTimestampBehaviorControls the return value for server timestamps that have not yet been set to their final value.
Friend classes
operator<<
friendstd::ostream&operator<<(std::ostream&out,constDocumentSnapshot&document)
Outputs the string representation of thisDocumentSnapshot to the given stream.
See also:ToString() for comments on the representation format.
Public functions
DocumentSnapshot
DocumentSnapshot()
Creates an invalidDocumentSnapshot that has to be reassigned before it can be used.
Calling any member function on an invalidDocumentSnapshot will be a no-op. If the function returns a value, it will return a zero, empty, or invalid value, depending on the type of the value.
DocumentSnapshot
DocumentSnapshot(constDocumentSnapshot&other)
Copy constructor.
DocumentSnapshot is immutable and can be efficiently copied (no deep copy is performed).
| Details | |||
|---|---|---|---|
| Parameters |
|
DocumentSnapshot
DocumentSnapshot(DocumentSnapshot&&other)
Move constructor.
Moving is more efficient than copying for aDocumentSnapshot. After being moved from, aDocumentSnapshot is equivalent to its default-constructed state.
| Details | |||
|---|---|---|---|
| Parameters |
|
Get
virtualFieldValueGet(constchar*field,ServerTimestampBehaviorstb)const
Retrieves a specific field from the document.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
| ||||
| Returns | The value contained in the field. If the field does not exist in the document, then a FieldValue instance withis_valid() == false will be returned. |
Get
virtualFieldValueGet(conststd::string&field,ServerTimestampBehaviorstb)const
Retrieves a specific field from the document.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
| ||||
| Returns | The value contained in the field. If the field does not exist in the document, then a FieldValue instance withis_valid() == false will be returned. |
Get
virtualFieldValueGet(constFieldPath&field,ServerTimestampBehaviorstb)const
Retrieves a specific field from the document.
| Details | |||||
|---|---|---|---|---|---|
| Parameters |
| ||||
| Returns | The value contained in the field. If the field does not exist in the document, then a FieldValue instance withis_valid() == false will be returned. |
GetData
virtualMapFieldValueGetData(ServerTimestampBehaviorstb)const
Retrieves all fields in the document as a map of FieldValues.
| Details | |||
|---|---|---|---|
| Parameters |
| ||
| Returns | A map containing all fields in the document, or an empty map if the document doesn't exist. |
ToString
std::stringToString()const
Returns a string representation of thisDocumentSnapshot for logging/debugging purposes.
exists
virtualboolexists()const
Explicitly verify a document's existence.
| Details | |
|---|---|
| Returns | True if the document exists in this snapshot. |
id
virtualconststd::string&id()const
Returns the string ID of the document for which thisDocumentSnapshot contains data.
| Details | |
|---|---|
| Returns | String ID of this document location. |
is_valid
boolis_valid()const
Returns true if thisDocumentSnapshot is valid, false if it is not valid.
An invalidDocumentSnapshot could be the result of:
- Creating a
DocumentSnapshotwith the default constructor. - Moving from the
DocumentSnapshot. - Deleting yourFirestore instance, which will invalidate all the
DocumentSnapshotinstances associated with it.
| Details | |
|---|---|
| Returns | true if this DocumentSnapshot is valid, false if thisDocumentSnapshot is invalid. |
metadata
virtualSnapshotMetadatametadata()const
Returns the metadata about this snapshot concerning its source and if it has local modifications.
| Details | |
|---|---|
| Returns | SnapshotMetadata about this snapshot. |
operator=
DocumentSnapshot&operator=(constDocumentSnapshot&other)
Copy assignment operator.
DocumentSnapshot is immutable and can be efficiently copied (no deep copy is performed).
| Details | |||
|---|---|---|---|
| Parameters |
| ||
| Returns | Reference to the destination DocumentSnapshot. |
operator=
DocumentSnapshot&operator=(DocumentSnapshot&&other)
Move assignment operator.
Moving is more efficient than copying for aDocumentSnapshot. After being moved from, aDocumentSnapshot is equivalent to its default-constructed state.
| Details | |||
|---|---|---|---|
| Parameters |
| ||
| Returns | Reference to the destination DocumentSnapshot. |
reference
virtualDocumentReferencereference()const
Returns the document location for which thisDocumentSnapshot contains data.
| Details | |
|---|---|
| Returns | DocumentReference of this document location. |
~DocumentSnapshot
virtual~DocumentSnapshot()
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-01-23 UTC.