Bird
Raised Fist0
Firebasecloud~20 mins

Creating collections and documents in Firebase - Practice Exercises

Choose your learning style10 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.

Practice

(1/5)
1. What is a collection in Firebase Firestore?
easy
A. A function to add data.
B. A single document storing data.
C. A group that holds multiple documents.
D. A method to delete documents.

Solution

  1. Step 1: Understand Firestore structure

    Firestore organizes data in collections and documents.
  2. Step 2: Define collection role

    A collection groups multiple documents together.
  3. Final Answer:

    A group that holds multiple documents. -> Option C
  4. Quick Check:

    Collection = group of documents [OK]
Hint: Collections hold documents, not single data items. [OK]
Common Mistakes:
  • Confusing collection with a single document
  • Thinking collection is a method
  • Mixing collection with data fields
2. Which Firebase Firestore method creates a document with an auto-generated ID?
easy
A. set()
B. add()
C. update()
D. get()

Solution

  1. Step 1: Recall document creation methods

    Firestore uses set() to create or overwrite documents with specific IDs.
  2. Step 2: Identify method for auto-generated IDs

    The add() method creates a document with an auto-generated ID inside a collection.
  3. Final Answer:

    add() -> Option B
  4. Quick Check:

    Auto-ID document creation = add() [OK]
Hint: Use add() for auto-generated document IDs. [OK]
Common Mistakes:
  • Using set() and expecting auto-ID
  • Confusing update() with creation
  • Using get() which only reads data
3. What will happen when this code runs?
db.collection('users').doc('user1').set({name: 'Anna', age: 25});
medium
A. Creates or overwrites 'user1' document with name and age.
B. Adds a new document with auto-generated ID.
C. Updates only the age field in 'user1' document.
D. Throws an error because set() needs add() instead.

Solution

  1. Step 1: Analyze method usage

    set() on a doc reference creates or overwrites that document.
  2. Step 2: Check document ID and data

    The doc ID is 'user1', so this document will be created or replaced with given data.
  3. Final Answer:

    Creates or overwrites 'user1' document with name and age. -> Option A
  4. Quick Check:

    set() on doc('user1') = overwrite/create [OK]
Hint: set() replaces document at given ID, add() creates new ID. [OK]
Common Mistakes:
  • Thinking add() is needed for specific IDs
  • Assuming update() is called by set()
  • Confusing set() with partial update
4. Identify the error in this Firestore code:
db.collection('products').add({name: 'Book', price: 10}).doc('p1').set({price: 12});
medium
A. No error, code works fine.
B. set() cannot update price field.
C. Collection name 'products' is invalid.
D. add() returns a promise, chaining doc() is invalid.

Solution

  1. Step 1: Understand add() return value

    add() returns a promise resolving to a document reference, not a direct reference.
  2. Step 2: Check chaining doc() after add()

    Calling doc() on the promise or document reference from add() is invalid syntax.
  3. Final Answer:

    add() returns a promise, chaining doc() is invalid. -> Option D
  4. Quick Check:

    add() returns promise, cannot chain doc() directly [OK]
Hint: add() returns promise; await before doc() calls. [OK]
Common Mistakes:
  • Chaining doc() immediately after add()
  • Ignoring promise returned by add()
  • Assuming set() cannot update fields
5. You want to create a collection 'orders' and add a document with a specific ID 'order123' containing {item: 'Pen', quantity: 3}. Which code snippet is correct?
hard
A. db.collection('orders').doc('order123').set({item: 'Pen', quantity: 3});
B. db.collection('orders').add({item: 'Pen', quantity: 3, id: 'order123'});
C. db.collection('orders').doc().set({item: 'Pen', quantity: 3, id: 'order123'});
D. db.collection('orders').set('order123', {item: 'Pen', quantity: 3});

Solution

  1. Step 1: Identify method to create document with specific ID

    Use doc('order123') to specify the document ID explicitly.
  2. Step 2: Use set() to create or overwrite document data

    set() writes the data to the specified document ID.
  3. Final Answer:

    db.collection('orders').doc('order123').set({item: 'Pen', quantity: 3}); -> Option A
  4. Quick Check:

    Specific ID document creation = doc(id).set() [OK]
Hint: Use doc('id').set() for specific document IDs. [OK]
Common Mistakes:
  • Using add() with id field (does not set document ID)
  • Calling doc() without ID for specific ID
  • Using set() directly on collection
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard