Create, read, and update a document
Create, read, update, and delete a document; Firestore stores flexible application records in collections.
Enable Firestore, exercise a unique document, and clean up through the SDK.
SDK dependencies
Run in a new example directory
python3 -m venv .venv
source .venv/bin/activate
python -m pip install google-cloud-firestore Save as hello.py
import os
from uuid import uuid4
from google.cloud import firestore
assert os.environ['FIRESTORE_EMULATOR_HOST']
client = firestore.Client(project=os.environ['GOOGLE_CLOUD_PROJECT'])
document = client.collection('guide').document(uuid4().hex)
try:
document.set({'message': 'hello'}, timeout=30)
message = document.get(timeout=30).get('message')
assert message == 'hello'
print(message)
document.update({'message': 'updated'}, timeout=30)
assert document.get(timeout=30).get('message') == 'updated'
finally:
document.delete(timeout=30)
client.close() Run the example
python hello.py Verify the result
The command completes successfully and prints the expected output shown below.
Expected output
hello