Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get an instance of Firestore.
Android Kotlin
val db = FirebaseFirestore.[1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'initialize()' instead of 'getInstance()'.
Trying to create Firestore with 'connect()'.
✗ Incorrect
The correct method to get a Firestore instance is getInstance().
2fill in blank
mediumComplete the code to add a new document to the 'users' collection.
Android Kotlin
db.collection("users").[1](userData)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get()' which only reads data.
Using 'update()' which requires an existing document.
✗ Incorrect
Use add() to add a new document with generated ID to a collection.
3fill in blank
hardFix the error in the code to listen for realtime updates on 'messages' collection.
Android Kotlin
db.collection("messages").addSnapshotListener { snapshots, [1] -> if (error != null) { return@addSnapshotListener } // process snapshots }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exception' or 'error' which are not the parameter names here.
Using 'failure' which is not defined.
✗ Incorrect
The listener callback uses throwable to check for problems.
4fill in blank
hardFill both blanks to update the 'age' field of a document with ID 'userId' in 'users' collection.
Android Kotlin
db.collection("users").document(userId).[1](mapOf("age" to [2]))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set()' which replaces the whole document.
Using a wrong age value.
✗ Incorrect
Use update() to change specific fields. The age value here is 25.
5fill in blank
hardFill all three blanks to query 'products' collection for items with price greater than 100 and order by price ascending.
Android Kotlin
db.collection("products").whereGreaterThan("[1]", [2]).orderBy("[3]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' instead of 'price' for filtering or ordering.
Using a wrong number for comparison.
✗ Incorrect
We filter by 'price' greater than 100 and order by 'price'.