Is there any query to match starting values of data field and retrieve data in firebase

Viewed 68

I am making an application in which i have to use CRUD operations for car data. The cars data is store in collection("CARS") and there are many data fields in a document.

we take a simple example

there are two cars named as 'audi' and 'BMW' in a collection

If i want to search a car with CarName and i Input "aud" in search text box by clicking the search button it will show me a car with a name of "audi" by matching its stating characters aud___ with car name

The solution of the problem i want is

  • Is there any query available in firestore which let me get the document by giving its starting characters in search text box
1 Answers

You can take advantage of the fact that the \uf8ff character is a very high code point in the Unicode range and is after most regular characters in Unicode, and write a query like:

  db.collection('CARS')
    .orderBy('carName')
    .startAt('aud')  // Looking for aud___
    .endAt('aud\uf8ff')
    .get()
    .then((querySnapshot) => {...})
    .catch(error => {
        console.log(error);
    });

Update following your comments: In the Firebase console, you should see the new index as follows (carType Ascending carName Ascending):

enter image description here

Related