0

Hello I´m searching for users this way

const findUsers = (name) => {
    return firebase
        .firestore()
        .collection("users")
        .where("name", "==", name)
        .get();
};

it works fine if I do findUsers("Michael Jackson") but it won´t work if I do findUsers("michael-jackson")

I´m using

const friendlyName = (string) => string.replace(/\W+/g, "-").toLowerCase();

to convert names to a friendly name that is easier to remember and good for sharing urls. I need to inverse that for searching? thank you!

handsome
  • 2,335
  • 7
  • 45
  • 73

1 Answers1

1

Firestore does not offer case insensitive string matches. When using the == operator, strings must match exactly.

If you wan to perform case insensitive searches, you should consider storing another field in each document with some canonical representation of the string, and require all the clients to use that canonical form when querying. For example, if you also store a field lowercaseName that contains "michael jackson", then your clients can use that field to search for that string in that specific all-lowercase format.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • too bad :( I guess I will have to create another key with the transformed name. is there a way to query the database to find only one record? looks like it returns a collection of docs even if is only one doc. – handsome Apr 29 '20 at 00:57
  • For filter queries, there is always a chance at getting more than one document, since there is nothing that ever forces a field value to be unique. – Doug Stevenson Apr 29 '20 at 01:08