0
0
Firebasecloud~20 mins

Getting a single document in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firestore Document Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
What is the output of this Firebase Firestore code snippet?

Consider the following Firebase Firestore code to get a single document by ID:

const docRef = firestore.collection('users').doc('user123');
const docSnap = await docRef.get();
if (docSnap.exists) {
  console.log(docSnap.data());
} else {
  console.log('No such document!');
}

What will be printed if the document with ID 'user123' exists and contains {name: 'Alice', age: 30}?

Firebase
const docRef = firestore.collection('users').doc('user123');
const docSnap = await docRef.get();
if (docSnap.exists) {
  console.log(docSnap.data());
} else {
  console.log('No such document!');
}
Anull
B"No such document!"
Cundefined
D{name: 'Alice', age: 30}
Attempts:
2 left
💡 Hint

Check what docSnap.data() returns when the document exists.

🧠 Conceptual
intermediate
1:30remaining
Which Firebase method retrieves a single document by its ID?

You want to get a single document from a Firestore collection by its ID. Which method should you use?

Acollection('users').doc('user123').get()
Bcollection('users').get()
Ccollection('users').where('id', '==', 'user123').get()
Dcollection('users').add({id: 'user123'})
Attempts:
2 left
💡 Hint

Think about how to directly access a document by its ID.

security
advanced
2:00remaining
What happens if Firestore security rules deny read access to a document you try to get?

You run this code to get a document:

const docSnap = await firestore.collection('users').doc('user123').get();

But your Firestore security rules deny read access to this document for your user. What will happen?

AThe get() call returns null without error.
BThe get() call returns an empty object {}.
CThe get() call throws a permission error and the document is not returned.
DThe get() call returns the document data but logs a warning.
Attempts:
2 left
💡 Hint

Consider how Firestore enforces security rules on reads.

Configuration
advanced
2:30remaining
Which code snippet correctly handles the case when a Firestore document does not exist?

You want to get a document and handle the case where it might not exist. Which snippet correctly does this?

A
const docSnap = await firestore.collection('users').doc('user123').get();
if (docSnap === null) {
  console.log('Document missing');
} else {
  console.log(docSnap.data());
}
B
const docSnap = await firestore.collection('users').doc('user123').get();
if (!docSnap.exists) {
  console.log('Document missing');
} else {
  console.log(docSnap.data());
}
C
const docSnap = await firestore.collection('users').doc('user123').get();
if (docSnap.data() === null) {
  console.log('Document missing');
} else {
  console.log(docSnap.data());
}
D
const docSnap = await firestore.collection('users').doc('user123').get();
if (docSnap.data() === undefined) {
  console.log('Document missing');
} else {
  console.log(docSnap.data());
}
Attempts:
2 left
💡 Hint

Check the property that indicates document existence.

Architecture
expert
3:00remaining
In a large app, which approach best optimizes reading a single Firestore document frequently?

You have a Firestore document that your app reads very often. Which approach best optimizes performance and cost?

AUse Firestore's offline persistence and cache the document locally to reduce reads.
BCall get() on the document every time you need it to ensure fresh data.
CUse a Cloud Function to read the document and send it to the client on every request.
DDuplicate the document data in Realtime Database for faster reads.
Attempts:
2 left
💡 Hint

Think about caching and offline capabilities Firestore offers.