Introduction and Context Recap

Welcome back! In the previous lessons, you learned how to structure and add data to your Firestore database using collections and documents. Today, we will focus on retrieving data from Firestore. Data retrieval is a fundamental part of working with any database, as it allows you to access and use the information you have stored. In this lesson, we will cover how to fetch a single document by its ID and how to retrieve multiple documents by their IDs. By the end of this session, you will be able to efficiently access the data you need from your Firestore collections.

Reading Data: Fetching a Single Document

Retrieving a document from Firestore is straightforward when you know the document's ID. For example, suppose you have a movies collection and want to fetch the document with the ID the-big-new-movie-2016. Here's how you can do it:

from google.cloud import firestore

db = firestore.Client()

# Reference to the document
doc_ref = db.collection('movies').document('the-big-new-movie-2016')

# Fetch the document
doc = doc_ref.get()
if doc.exists:
    print("Movie found:", doc.to_dict())
else:
    print("Movie not found.")

Output (example):

Movie found: {'title': 'The Big New Movie', 'genre': 'Action', 'year': 2016}

If you only need specific fields from the document, you can use the field_paths parameter to select them:

# Fetch only specific fields
doc = doc_ref.get(field_paths=['title', 'genre'])
if doc.exists:
    print("Selected fields of the movie:", doc.to_dict())

Output (example):

Selected fields of the movie: {'title': 'The Big New Movie', 'genre': 'Action'}

This approach allows you to minimize the amount of data transferred and focus only on the information you need.

Retrieving Multiple Documents by IDs

Sometimes, you may need to fetch several documents at once, given their IDs. Firestore provides a convenient way to do this using the get_all method on the Firestore client. You simply create references to each document you want to retrieve and pass them to the method. Here's an example:

# List of document IDs to fetch
doc_ids = [
    'the-big-new-movie-2016',
    'the-bigger-newer-movie-2017'
]

# Create document references
doc_refs = [db.collection('movies').document(doc_id) for doc_id in doc_ids]

# Use the client's get_all method to fetch all documents in one request
docs = db.get_all(doc_refs)

for doc in docs:
    if doc.exists:
        print("Movie found:", doc.id, doc.to_dict())
    else:
        print("Movie not found:", doc.id)

Output (example):

Movie found: the-big-new-movie-2016 {'title': 'The Big New Movie', 'genre': 'Action', 'year': 2016}
Movie found: the-bigger-newer-movie-2017 {'title': 'The Bigger Newer Movie', 'genre': 'Comedy', 'year': 2017}

Note that get_all is a method on the Firestore client (db), not on a collection. This method allows you to efficiently retrieve multiple documents in a single request, even if they're from different collections, as long as you know their document references.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal