Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use startAt() and endAt() in Cloud Firestore?

I read Dan McGrath's answer from this question in which he says that in Firestore there is an equivalent for the following line of code:

myRef.startAt(searchText).endAt(searchText + "\uf8ff");

This line works perfect in Firebase Realtime database but I'm trying to find the equivalent in Cloud Firestore for days. Can anyone help me with that?

Thanks in advance!

like image 248
Joan P. Avatar asked Mar 11 '18 09:03

Joan P.


People also ask

How do I use Startbafter in firebase?

Use the startAt() or startAfter() methods to define the start point for a query. The startAt() method includes the start point, while the startAfter() method excludes it. For example, if you use startAt(A) in a query, it returns the entire alphabet. If you use startAfter(A) instead, it returns B-Z .

How do references work in firestore?

A DocumentReference refers to a document location in a Firestore database and can be used to write, read, or listen to the location. The document at the referenced location may or may not exist. A DocumentReference can also be used to create a CollectionReference to a subcollection.

What does firebase firestore () do?

Cloud Firestore caches data that your app is actively using, so the app can write, read, listen to, and query data even if the device is offline.


2 Answers

You have a couple of options.

Query filters

You can add query filters using the .where keyword. Take a look at Order and Limit Data with Cloud Firestore

Query cursors

If you want to use cursors for paginating data, take a look at Paginate Data with Query Cursors

Your query

You need to specify which field you are wanting to search. For example, if you're looking for all documents with a name containing the searchText you will need to add an orderBy parameter.

myRef.orderBy("name").startAt(searchText).endAt(searchText + "\uf8ff");

This assumes that your myRef is a reference to a collection.

like image 100
Jason Berryman Avatar answered Sep 21 '22 01:09

Jason Berryman


you find the solution, maybe you confuse, just change this " with single quotes (') and it will works perfectly thanks for the other post it helps me too. I leave my code here:

FirestoreRecyclerOptions <Ingreso> newoptions = new FirestoreRecyclerOptions.Builder<Ingreso>()
                .setQuery(fStore.collection("DatosEmpresa").orderBy("DepartamentoEmpresaLoweCase").startAt(newText.toLowerCase()).limit(25).endAt(newText.toLowerCase()+'\uf8ff'),Ingreso.class)
                .build();
like image 40
Antony Nicolas Huaman Alikhan Avatar answered Sep 18 '22 01:09

Antony Nicolas Huaman Alikhan