0
0
Firebasecloud~20 mins

Creating collections and documents in Firebase - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firestore Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
What happens when you add a document without specifying an ID?

In Firebase Firestore, if you add a document to a collection without specifying an ID, what will be the result?

Firebase
db.collection('users').add({name: 'Alice', age: 30});
AFirestore automatically generates a unique ID for the new document.
BThe document is added with an empty string as its ID.
CThe operation fails with an error because ID is required.
DThe document overwrites the first document in the collection.
Attempts:
2 left
💡 Hint

Think about how Firestore handles document IDs when none are provided.

Configuration
intermediate
2:00remaining
Which code snippet correctly creates a collection and a document with a custom ID?

Choose the code that creates a collection named products and adds a document with ID prod123 containing a field price set to 100.

Adb.collection('products').doc().set({id: 'prod123', price: 100});
Bdb.collection('products').add({id: 'prod123', price: 100});
Cdb.collection('products').doc('prod123').set({price: 100});
Ddb.doc('products/prod123').add({price: 100});
Attempts:
2 left
💡 Hint

Remember that doc() with an argument sets the document ID explicitly.

Architecture
advanced
2:00remaining
How does Firestore organize data when you create nested collections?

Consider you have a collection users, each user document has a subcollection orders. How is this data organized in Firestore?

ASubcollections are separate collections linked by path, not nested inside documents.
BEach <code>orders</code> subcollection is stored inside its parent user document as a nested object.
CAll subcollections are merged into the main <code>users</code> collection.
DFirestore duplicates subcollection data inside the parent document for faster access.
Attempts:
2 left
💡 Hint

Think about how Firestore paths work for collections and documents.

security
advanced
2:00remaining
Which Firestore security rule allows only authenticated users to create documents in the 'messages' collection?

Choose the correct Firestore security rule snippet that permits only signed-in users to add documents to the messages collection.

Amatch /messages/{msgId} { allow create: if request.auth == null; }
Bmatch /messages/{msgId} { allow create: if request.auth != null; }
Cmatch /messages/{msgId} { allow create: if true; }
Dmatch /messages/{msgId} { allow create: if request.auth.uid == ''; }
Attempts:
2 left
💡 Hint

Check how to verify if a user is signed in using request.auth.

Best Practice
expert
3:00remaining
What is the best practice for structuring Firestore collections to optimize read performance for user profiles and their posts?

You want to design Firestore data for users and their posts. Which approach optimizes read performance and scalability?

ADuplicate posts data inside both user documents and a posts collection.
BStore all posts as a subcollection under each user document.
CEmbed all posts inside the user document as an array.
DStore all posts in a single top-level collection with a userId field to filter posts.
Attempts:
2 left
💡 Hint

Consider Firestore limits on document size and query efficiency.