I have a simple Users collection in my Firestore data, that has few properties. this is how it looks:
I'm trying to implement a search option in my application, and I want it to be able to get a substring too. So if I have two users, for example:
demouser
hellouser
demotest
And I search for "demo" it will give me both, demouser
and demotest
.
I searched, but found only old answer (an year or so old) and saw that it is not possible in Firestore, This is what I tried from an answer I saw:
func searchForName(find name: String, completion: @escaping([User]?)->()){
var usersFound: [User] = []
db.collection(USERS_COLLECTION).whereField(USER_NAME, isGreaterThanOrEqualTo: name).whereField(USER_NAME, isLessThanOrEqualTo: name).getDocuments { (snapshot, err) in
if err == nil {
guard let results = snapshot?.documents else { return }
print(results[0].get(USER_MAIL))
}
}
}
But it didn't work. I tried with arrayContains
too, but got no results at all.
Is there any option to find a substring of a field in Firestore at all?
EDIT: My current code is based on the answer from here: Google Firestore: Query on substring of a property value (text search) which does not work for me.