Home » Android » android – Firestore query a document based on starts with in any of the 2 fields-Exceptionshub

android – Firestore query a document based on starts with in any of the 2 fields-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

My Firestore set up is the following:

Collection : Users |
DocumentID : [Auto-generated] |
Fields ->
name: robert downey jr,
username: iron_man

I would like to query this document with a search query like “robert” or “rob” or “iron” or “ir”. How do I do this in Firestore? I tried whereArrayContains() but that query looks for perfect match rather than starts with

How to&Answers:

You won’t be able to do this in one query, because Firestore does not support logical OR queries that span multiple fields. What you can do instead is make two queries, one for name, and the other for username that do string prefix sorts (read other questions about that here, here, here). Then, after you do both queries, merge the results on the client to build the final list of results.