Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL LIKE operator in Cloud Firestore?

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.

like image 845
Manjok Avatar asked Oct 09 '17 09:10

Manjok


People also ask

Can you use SQL on firestore?

FireSQL is a library built on top of the official Firebase SDK that allows you to query Cloud Firestore using SQL syntax. It's smart enough to issue the minimum amount of queries necessary to the Firestore servers in order to get the data that you request.

Can you use SQL in Firebase?

It is not possible to use Firebase in this way. Firebase is a real-time object store. It is not a SQL database and is not intended to be a replacement for one. It completely lacks mechanisms such as JOINs, WHERE query filters, foreign keys, and other tools relational databases all provide.

How do I get specific data from firestore?

Firestore provides powerful query functionality for specifying which documents you want to retrieve from a collection or collection group. These queries can also be used with either get() or addSnapshotListener() , as described in Get Data and Get Realtime Updates.

How do I query multiple collections in firestore?

Firestore does not have the concept of server-side joins or projections across collections. Each query or document read can only take data from a single collection, or from all collections that have the same name with collection group queries.


2 Answers

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') 
like image 199
Joan P. Avatar answered Oct 14 '22 00:10

Joan P.


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 <.

like image 38
Dan McGrath Avatar answered Oct 14 '22 01:10

Dan McGrath