javaandroidfirebasegoogle-cloud-firestore

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!


Solution

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