31
votes

I have been using Firebase Real Time Fatabase for a while and I come across Cloud Firestore today. I can't figure out on how to use LIKE operator on Firestore.

Firebase Real Time Database

ref.child('user').orderByChild('name').startAt(name).endAt(name+'\uf8ff')

On Cloud Firestore, I have tried

userRef.where('name', '>=', name); <br>
userRef.where('name', '<=', name);

But it doesn't work.

4

4 Answers

33
votes

To solve this, you need to change orderByChild function with orderBy. So please use the following code:

ref.collection('user').orderBy('name').startAt(name).endAt(name+'\uf8ff')
6
votes

There isn't an equivalent to LIKE, but you can do prefix filtering in the same way you do it in RTDB.

The query you have written is the same as equals. You need to do the same end by trick and do just less than <.

2
votes

There's no such operator, allowed ones are ==, <, <=, >, >=. Here you can find all the limitations of queries in cloud firestore: https://firebase.google.com/docs/firestore/query-data/queries

2
votes

FYI: With the later versions of cloud firestore (e.g. 0.12.5), the startAt() and endAt() methods require a list of strings, not a single string.