I have a pretty large collection of documents in Firestore with random IDs and each one of those documents has two fields, a name and a description. I want my app to allow the user to enter a series of characters and then present to him all the documents which contain that sequence of chars in let's say the name field. Is this feasible using Firebase & its queries and if so, could you give me a code example in Kotlin or Java? I know there are some methods such as hasChild() or child("child name").exists() but since i don't yet know which document the user is looking for i can't use them if i'm not mistaken.
For example, if i had a collection of 3 documents which had the following names ("mike","michael","dave") and the user entered "mi", i'd like to be able to retrieve the documents whose names are "mike" & "michael".
If you want to get all documents where the name field starts with mi, you can do so with:
db.collection("users")
.whereGreaterThanOrEqualTo("name", "mi")
.whereLessThanOrEqualTo("name", "mi\uF7FF")
.get()
.addOnSuccessListener { documents ->
for (document in documents) {
Log.d(TAG, "${document.id} => ${document.data}")
}
}
.addOnFailureListener { exception ->
Log.w(TAG, "Error getting documents: ", exception)
}
The \uF7FF value used here is the last Unicode character that exists, so this:
name valuemimiFor much more on this, read the Firebase documentation on querying data.
I also recommend checking out these related questions:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With