Reference documentation and code samples for the Cloud Firestore API class Google::Cloud::Firestore::DocumentSnapshot.
DocumentSnapshot
A document snapshot object is an immutable representation for a document in a Cloud Firestore database.
The snapshot can reference a non-existing document.
See Google::Cloud::Firestore::DocumentReference#get, Google::Cloud::Firestore::DocumentReference#listen, Query#get, Query#listen, and QuerySnapshot#docs.
Inherits
- Object
Examples
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new # Get a document snapshot nyc_snap = firestore.doc("cities/NYC").get # Get the document data nyc_snap[:population] #=> 1000000
Listen to a document reference for changes:
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new # Get a document reference nyc_ref = firestore.doc "cities/NYC" listener = nyc_ref.listen do |snapshot| puts "The population of #{snapshot[:name]} is #{snapshot[:population]}." end # When ready, stop the listen operation and close the stream. listener.stop
Methods
#[]
def [](field_path) -> Object
Retrieves the document data.
- (Object) — The data at the field path.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new nyc_snap = firestore.doc("cities/NYC").get nyc_snap.get(:population) #=> 1000000
Accessing data using []:
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new nyc_snap = firestore.doc("cities/NYC").get nyc_snap[:population] #=> 1000000
Nested data can be accessing with field path:
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new frank_snap = firestore.doc("users/frank").get frank_snap.get("favorites.food") #=> "Pizza"
Nested data can be accessing with FieldPath object:
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new user_snap = firestore.doc("users/frank").get nested_field_path = firestore.field_path :favorites, :food user_snap.get(nested_field_path) #=> "Pizza"
#create_time
def create_time() -> Time
The time at which the document was created.
This value increases when a document is deleted then recreated.
- (Time) — The time the document was was created
#created_at
def created_at() -> Time
The time at which the document was created.
This value increases when a document is deleted then recreated.
- (Time) — The time the document was was created
#data
def data() -> Hash, nil
Retrieves the document data. When the document exists the data hash is
frozen and will not allow any changes. When the document does not
exist nil
will be returned.
- (Hash, nil) — The document data.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new nyc_snap = firestore.doc("cities/NYC").get # Get the document data nyc_snap.data[:population] #=> 1000000
#document_id
def document_id() -> String
The document identifier for the document snapshot.
- (String) — document identifier.
#document_path
def document_path() -> String
A string representing the path of the document, relative to the document root of the database.
- (String) — document path.
#exists?
def exists?() -> Boolean
Determines whether the document exists.
- (Boolean) — Whether the document exists.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new nyc_snap = firestore.doc("cities/NYC").get # Does NYC exist? nyc_snap.exists? #=> true
#fields
def fields() -> Hash, nil
Retrieves the document data. When the document exists the data hash is
frozen and will not allow any changes. When the document does not
exist nil
will be returned.
- (Hash, nil) — The document data.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new nyc_snap = firestore.doc("cities/NYC").get # Get the document data nyc_snap.data[:population] #=> 1000000
#get
def get(field_path) -> Object
Retrieves the document data.
- (Object) — The data at the field path.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new nyc_snap = firestore.doc("cities/NYC").get nyc_snap.get(:population) #=> 1000000
Accessing data using []:
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new nyc_snap = firestore.doc("cities/NYC").get nyc_snap[:population] #=> 1000000
Nested data can be accessing with field path:
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new frank_snap = firestore.doc("users/frank").get frank_snap.get("favorites.food") #=> "Pizza"
Nested data can be accessing with FieldPath object:
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new user_snap = firestore.doc("users/frank").get nested_field_path = firestore.field_path :favorites, :food user_snap.get(nested_field_path) #=> "Pizza"
#missing?
def missing?() -> Boolean
Determines whether the document is missing.
- (Boolean) — Whether the document is missing.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new atlantis_snap = firestore.doc("cities/Atlantis").get # Does Atlantis exist? atlantis_snap.missing? #=> true
#parent
def parent() -> CollectionReference
The collection the document snapshot belongs to.
- (CollectionReference) — parent collection.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new # Get a document snapshot nyc_snap = firestore.doc("cities/NYC").get # Get the document's parent collection cities_col = nyc_snap.parent
#read_at
def read_at() -> Time
The time at which the document was read.
This value is set even if the document does not exist.
- (Time) — The time the document was read
#read_time
def read_time() -> Time
The time at which the document was read.
This value is set even if the document does not exist.
- (Time) — The time the document was read
#ref
def ref() -> DocumentReference
The document reference object for the data.
- (DocumentReference) — document reference.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new # Get a document snapshot nyc_snap = firestore.doc("cities/NYC").get # Get the document reference nyc_ref = nyc_snap.ref
#reference
def reference() -> DocumentReference
The document reference object for the data.
- (DocumentReference) — document reference.
require "google/cloud/firestore" firestore = Google::Cloud::Firestore.new # Get a document snapshot nyc_snap = firestore.doc("cities/NYC").get # Get the document reference nyc_ref = nyc_snap.ref
#update_time
def update_time() -> Time
The time at which the document was last changed.
This value is initally set to the created_at
on document creation,
and increases each time the document is updated.
- (Time) — The time the document was was last changed
#updated_at
def updated_at() -> Time
The time at which the document was last changed.
This value is initally set to the created_at
on document creation,
and increases each time the document is updated.
- (Time) — The time the document was was last changed