firebase::firestore::DocumentSnapshot

#include <document_snapshot.h>

A DocumentSnapshot contains data read from a document in your Firestore database.

Summary

The data can be extracted with the GetData() method, or by using Get() to access a specific field. For a DocumentSnapshot that points to a non-existing document, any data access will cause a failed assertion. You can use the exists() method to explicitly verify a document's existence.

Constructors and Destructors

DocumentSnapshot()
Creates an invalid DocumentSnapshot that has to be reassigned before it can be used.
DocumentSnapshot(const DocumentSnapshot & other)
Copy constructor.
DocumentSnapshot(DocumentSnapshot && other)
Move constructor.
~DocumentSnapshot()

Public types

ServerTimestampBehavior{
  kNone = 0,
  kEstimate,
  kPrevious,
  kDefault = 0
}
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
virtual FieldValue
Retrieves a specific field from the document.
Get(const std::string & field, ServerTimestampBehavior stb) const
virtual FieldValue
Retrieves a specific field from the document.
Get(const FieldPath & field, ServerTimestampBehavior stb) const
virtual FieldValue
Retrieves a specific field from the document.
GetData(ServerTimestampBehavior stb) const
virtual MapFieldValue
Retrieves all fields in the document as a map of FieldValues.
ToString() const
std::string
Returns a string representation of this DocumentSnapshot for logging/debugging purposes.
exists() const
virtual bool
Explicitly verify a document's existence.
id() const
virtual const std::string &
Returns the string ID of the document for which this DocumentSnapshot contains data.
is_valid() const
bool
Returns true if this DocumentSnapshot is valid, false if it is not valid.
metadata() const
Returns the metadata about this snapshot concerning its source and if it has local modifications.
operator=(const DocumentSnapshot & other)
Copy assignment operator.
operator=(DocumentSnapshot && other)
Move assignment operator.
reference() const
Returns the document location for which this DocumentSnapshot contains data.

Public types

ServerTimestampBehavior

 ServerTimestampBehavior

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

Properties
kDefault

The default behavior, which is equivalent to specifying kNone.

kEstimate

Return local estimates for server timestamps that have not yet been set to their final value.

This estimate will likely differ from the final value and may cause these pending values to change once the server result becomes available.

kNone

Return Null for server timestamps that have not yet been set to their final value.

kPrevious

Return the previous value for server timestamps that have not yet been set to their final value.

Friend classes

operator<<

friend std::ostream & operator<<(std::ostream &out, const DocumentSnapshot &document)

Outputs the string representation of this DocumentSnapshot to the given stream.

See also:ToString() for comments on the representation format.

Public functions

DocumentSnapshot

 DocumentSnapshot()

Creates an invalid DocumentSnapshot that has to be reassigned before it can be used.

Calling any member function on an invalid DocumentSnapshot 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(
  const DocumentSnapshot & other
)

Copy constructor.

DocumentSnapshot is immutable and can be efficiently copied (no deep copy is performed).

Details
Parameters
other
DocumentSnapshot to copy from.

DocumentSnapshot

 DocumentSnapshot(
  DocumentSnapshot && other
)

Move constructor.

Moving is more efficient than copying for a DocumentSnapshot. After being moved from, a DocumentSnapshot is equivalent to its default-constructed state.

Details
Parameters
other
DocumentSnapshot to move data from.

Get

virtual FieldValue Get(
  const char *field,
  ServerTimestampBehavior stb
) const 

Retrieves a specific field from the document.

Details
Parameters
field
String ID of the field to retrieve. The pointer only needs to be valid during this call.
stb
Configures how server timestamps that have not yet been set to their final value are returned from the snapshot (optional).
Returns
The value contained in the field. If the field does not exist in the document, then a FieldValue instance with is_valid() == false will be returned.

Get

virtual FieldValue Get(
  const std::string & field,
  ServerTimestampBehavior stb
) const 

Retrieves a specific field from the document.

Details
Parameters
field
String ID of the field to retrieve.
stb
Configures how server timestamps that have not yet been set to their final value are returned from the snapshot (optional).
Returns
The value contained in the field. If the field does not exist in the document, then a FieldValue instance with is_valid() == false will be returned.

Get

virtual FieldValue Get(
  const FieldPath & field,
  ServerTimestampBehavior stb
) const 

Retrieves a specific field from the document.

Details
Parameters
field
Path of the field to retrieve.
stb
Configures how server timestamps that have not yet been set to their final value are returned from the snapshot (optional).
Returns
The value contained in the field. If the field does not exist in the document, then a FieldValue instance with is_valid() == false will be returned.

GetData

virtual MapFieldValue GetData(
  ServerTimestampBehavior stb
) const 

Retrieves all fields in the document as a map of FieldValues.

Details
Parameters
stb
Configures how server timestamps that have not yet been set to their final value are returned from the snapshot (optional).
Returns
A map containing all fields in the document, or an empty map if the document doesn't exist.

ToString

std::string ToString() const 

Returns a string representation of this DocumentSnapshot for logging/debugging purposes.

exists

virtual bool exists() const 

Explicitly verify a document's existence.

Details
Returns
True if the document exists in this snapshot.

id

virtual const std::string & id() const 

Returns the string ID of the document for which this DocumentSnapshot contains data.

Details
Returns
String ID of this document location.

is_valid

bool is_valid() const 

Returns true if this DocumentSnapshot is valid, false if it is not valid.

An invalid DocumentSnapshot could be the result of:

Details
Returns
true if this DocumentSnapshot is valid, false if this DocumentSnapshot is invalid.

metadata

virtual SnapshotMetadata metadata() 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=(
  const DocumentSnapshot & other
)

Copy assignment operator.

DocumentSnapshot is immutable and can be efficiently copied (no deep copy is performed).

Details
Parameters
other
DocumentSnapshot to copy from.
Returns
Reference to the destination DocumentSnapshot.

operator=

DocumentSnapshot & operator=(
  DocumentSnapshot && other
)

Move assignment operator.

Moving is more efficient than copying for a DocumentSnapshot. After being moved from, a DocumentSnapshot is equivalent to its default-constructed state.

Details
Parameters
other
DocumentSnapshot to move data from.
Returns
Reference to the destination DocumentSnapshot.

reference

virtual DocumentReference reference() const 

Returns the document location for which this DocumentSnapshot contains data.

Details
Returns
DocumentReference of this document location.

~DocumentSnapshot

virtual  ~DocumentSnapshot()