Bird
Raised Fist0
Firebasecloud~5 mins

Document ID strategies in Firebase - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you store data in Firebase Firestore, each piece of data is saved as a document with a unique ID. Choosing how to create these IDs helps keep your data organized and easy to find.
When you want Firebase to create a unique ID automatically for each new document.
When you want to use a meaningful ID like a username or email to find documents easily.
When you want to avoid duplicate documents by using a fixed ID for each entry.
When you want to group related data by using IDs that follow a pattern.
When you want to keep document IDs short and simple for quick access.
Commands
This command adds a new document to the 'users' collection with an automatically generated unique ID.
Terminal
firebase firestore:documents:add users --data '{"name":"Alice","age":30}'
Expected OutputExpected
Document added with ID: XyZ123AbC
This command creates or updates a document with the fixed ID 'alice123' in the 'users' collection.
Terminal
firebase firestore:documents:set users/alice123 --data '{"name":"Alice","age":30}'
Expected OutputExpected
Document users/alice123 created or updated.
This command retrieves the document with the ID 'alice123' from the 'users' collection to verify it exists.
Terminal
firebase firestore:documents:get users/alice123
Expected OutputExpected
{ "name": "Alice", "age": 30 }
Key Concept

If you remember nothing else from this pattern, remember: choosing the right document ID helps you find and manage your data easily and prevents duplicates.

Common Mistakes
Using random IDs when you need to find documents by a known key.
It makes it hard to locate or update specific documents later.
Use meaningful fixed IDs like usernames or emails when you want easy access.
Manually creating IDs that are not unique.
This causes data to overwrite or conflicts in your database.
Use Firebase's automatic ID generation or ensure your custom IDs are unique.
Summary
Use Firebase's automatic ID generation for unique, random document IDs.
Use fixed, meaningful IDs when you want to find or update documents easily.
Always verify document creation by retrieving the document after adding or updating.

Practice

(1/5)
1. What is the main purpose of a document ID in Firebase?
easy
A. To uniquely identify each record in a collection
B. To store user passwords securely
C. To define the database location
D. To control access permissions

Solution

  1. Step 1: Understand what a document ID represents

    A document ID is a unique identifier for each record in a Firebase collection, ensuring each document can be accessed individually.
  2. Step 2: Differentiate from other database concepts

    Document IDs are not used for storing passwords, defining database location, or controlling access permissions.
  3. Final Answer:

    To uniquely identify each record in a collection -> Option A
  4. Quick Check:

    Document ID = Unique record identifier [OK]
Hint: Document ID uniquely names each record [OK]
Common Mistakes:
  • Confusing document ID with security rules
  • Thinking document ID stores data
  • Mixing document ID with database path
2. Which of the following is the correct way to let Firebase generate a document ID automatically?
easy
A. Use collectionRef.doc() without arguments
B. Use collectionRef.doc('customID')
C. Use collectionRef.add(data)
D. Use collectionRef.set(data)

Solution

  1. Step 1: Identify Firebase methods for adding documents

    The add() method adds a document with an auto-generated ID to a collection.
  2. Step 2: Understand difference from doc() and set()

    doc() with no argument creates a reference but does not add data; set() requires a document reference.
  3. Final Answer:

    Use collectionRef.add(data) -> Option C
  4. Quick Check:

    Auto ID creation = add() method [OK]
Hint: Use add() to auto-generate document ID [OK]
Common Mistakes:
  • Using doc() without set() does not create document
  • Assuming set() auto-generates ID
  • Passing custom ID when auto ID is needed
3. Given this code snippet, what will be the document ID of the new document?
const docRef = collectionRef.doc('user123');
docRef.set({name: 'Alice'});
medium
A. 'user123'
B. Auto-generated random ID
C. 'Alice'
D. Undefined

Solution

  1. Step 1: Analyze the doc() method argument

    The doc('user123') call creates a document reference with ID 'user123'.
  2. Step 2: Understand set() usage

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

    'user123' -> Option A
  4. Quick Check:

    doc('user123') sets document ID = 'user123' [OK]
Hint: doc('ID') sets document ID explicitly [OK]
Common Mistakes:
  • Thinking set() changes the ID
  • Assuming ID is auto-generated here
  • Confusing document data with ID
4. What is wrong with this code if the goal is to create a document with an auto-generated ID?
const docRef = collectionRef.doc();
medium
A. doc() requires a string argument
B. doc() without arguments does not create a document automatically
C. set() cannot be used with doc()
D. The data object is missing an ID field

Solution

  1. Step 1: Understand doc() without arguments

    Calling doc() without arguments creates a reference with a random ID but does not create the document automatically.
  2. Step 2: Confirm the issue

    The code only creates a DocumentReference; to create the document, set(data) or add(data) is needed.
  3. Final Answer:

    doc() without arguments does not create a document automatically -> Option B
  4. Quick Check:

    doc() alone ≠ document creation [OK]
Hint: doc() alone doesn't create document, set() must follow [OK]
Common Mistakes:
  • Assuming doc() creates document immediately
  • Thinking set() is invalid here
  • Expecting doc() to require argument always
5. You want to create user profiles with document IDs matching their usernames for easy lookup. Which strategy is best?
hard
A. Create documents without IDs and rely on collection order
B. Let Firebase auto-generate IDs and store username inside document
C. Use random UUIDs as document IDs and index usernames separately
D. Use usernames as document IDs by calling doc(username)

Solution

  1. Step 1: Understand the goal

    The goal is to have document IDs that match usernames for easy direct lookup.
  2. Step 2: Evaluate strategies

    Using doc(username) sets the document ID to the username, enabling direct access by username.
  3. Step 3: Compare with other options

    Auto-generated IDs or random UUIDs require extra queries to find username; no IDs or relying on order is unreliable.
  4. Final Answer:

    Use usernames as document IDs by calling doc(username) -> Option D
  5. Quick Check:

    Username as ID = direct lookup [OK]
Hint: Use username as doc ID for direct access [OK]
Common Mistakes:
  • Relying on auto IDs for username lookup
  • Ignoring need for unique IDs
  • Assuming collection order is stable
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