Welcome to the next step in your cloud development journey! So far, you have learned how to securely access cloud services from virtual machine instances and how to work with GCP client libraries using Firestore for basic connectivity testing. In this lesson, we will dive deeper into Cloud Firestore, a fully managed NoSQL document database service.
Cloud Firestore is designed for fast and flexible data storage and retrieval. It is commonly used for applications that need to handle large amounts of structured data with low latency, such as user profiles, session data, or product catalogs. By the end of this lesson, you will know how to create a collection in Firestore and perform the four basic operations: Create, Read, Update, and Delete (CRUD).
Let's get started!
In Cloud Firestore, data is organized into collections. Each collection contains documents, and each document holds a set of key-value pairs. Collections are created automatically when you add your first document to them.
To create a collection named users_<random> and add your first document, you can use the following approach:
- The
uuidlibrary generates universally unique identifiers. Here,uuid.uuid4().hex[:8]creates a short random suffix like1a2b3c4dto make the collection name unique and avoid conflicts when multiple users run the same code. COLLECTION_NAMEuses an f-string to combine the base nameusers_with the random suffix.db.collection(COLLECTION_NAME).document("u-123")creates a reference to a document with the IDu-123in the collection..set({...})creates the document with the provided data.
Sample Output:
Now that you have a collection and a document, let’s go through the four basic operations: Create, Read, Update, and Delete.
To add a new document to your collection, use the set method with a document reference:
This adds a user with the ID u-456, name Grace, and email grace@example.com to the collection.
To get a document by its ID, use the get method:
Sample Output:
To update a document, use the update method. For example, to change the email address:
Sample Output:
To delete a document, use the delete method:
Sample Output:
While Cloud Firestore does not provide a direct method to delete an entire collection in a single call, you can delete all documents in the collection to clean up your resources. Here’s how you can do it:
Sample Output:
Cleaning up unused collections helps keep your environment organized and prevents unnecessary resource usage.
In this lesson, you learned how to work with Cloud Firestore by:
- Creating a collection and adding documents
- Performing create, read, update, and delete operations on documents
- Cleaning up by deleting all documents in a collection
You saw step-by-step code examples and learned what each part does. In the next practice exercises, you will get hands-on experience with these operations. Remember to pay attention to collection names and document IDs, and always clean up your resources when you are done. Good luck, and enjoy working with Cloud Firestore!
