1

I have a simple Users collection in my Firestore data, that has few properties. this is how it looks: enter image description here

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.

John Doah
  • 1,839
  • 7
  • 25
  • 46
  • Interesting question. This is a great question for Doug Stevenson, though I don't think I can cc him in a comment. I'll look into and let you know if I find anything. – David Chopin Oct 14 '19 at 20:56
  • Still not possible to do substring searches (except string prefixes with the `>` operator). That's not likely to change in the near future, so I'd suggest using another database alongside Firestore that can do this, though it won't be as scalable as Firestore. – Doug Stevenson Oct 14 '19 at 21:02

0 Answers0