7

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!

Joan P.
  • 2,368
  • 6
  • 30
  • 63

2 Answers2

11

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.

Jason Berryman
  • 4,760
  • 1
  • 26
  • 42
  • Thanks Jason for your answer. Can you please provide me the exact equivalent in Firestore? Thanks! – Joan P. Mar 11 '18 at 18:37
  • I've updated my response to add the `orderBy` parameter, which you are missing. – Jason Berryman Mar 11 '18 at 20:38
  • 2
    @Jason Berryman - How can I make startAt and endAt case insensitive? – umesh lohani Dec 27 '18 at 19:35
  • 1
    @umeshlohani - This is not possible, as all entries are case sensitive. This may be a good use case for a full text search service, such as Algolia. You could create a separate `search` collection which contains both uppercased and lowercased entries, with pointers to the original document. When a new document is added to the main collection, trigger a Cloud Function to add upper and lower cased versions to the `search` collection – Jason Berryman Jan 09 '19 at 00:28
1

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();
Roshin Raphel
  • 2,612
  • 4
  • 22
  • 40