0

I am making a query in a class my problem is that I have some license plates, here in Colombia licenses plates are as it follows ABC 123. I need my query to only return to me, in this case, the ones that end with 2

This is what i did following the documentation...

private void queryForPlaca(String terminaEn){

    ParseQuery<ParseObject> query = ParseQuery.getQuery("TestDrive");
    query.whereFullText("Placa", terminaEn);
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            for (ParseObject obj:objects
                 ) {
                listaDeVehiculos.add(obj);
            }
            ListaVehiculosPicoYPlacaAdapter adapter= new ListaVehiculosPicoYPlacaAdapter(getActivity(),listaDeVehiculos);
            listadoTestDrive.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    });
}

terminaEn variable querys for the licenses places plates depending on the day selected in a calendar (in this case 2). My problem is that not only my listaDeVehiculos is not returning any values and the other problem that I see is that it also returns values I don't need for example ABS 124, AXX 524, AEE 234 etc. How should i modify my query?.

Felipe Franco
  • 171
  • 3
  • 15

1 Answers1

0

You won't be able to do that using text search, since MongoDB text search feature only matches complete terms. You will have to do that using regular expressions. Try the following:

private void queryForPlaca(String terminaEn){

    ParseQuery<ParseObject> query = ParseQuery.getQuery("TestDrive");
    query.whereMatches("Placa", terminaEn + "$");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            for (ParseObject obj:objects) {
                listaDeVehiculos.add(obj);
            }
            ListaVehiculosPicoYPlacaAdapter adapter= new ListaVehiculosPicoYPlacaAdapter(getActivity(),listaDeVehiculos);
            listadoTestDrive.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    });
}

Please have in mind that regular expressions are very expensive to the database, so it's important for you to create the appropriate index. A { Placa: 1 } index in the TestDrive collection should help you a lot. For your reference, please take a look here: https://docs.parseplatform.org/android/guide/#regular-expressions

Davi Macêdo
  • 2,954
  • 1
  • 8
  • 11
  • thanks davi im pretty confused with this especially with the terminaEn+"$". Can you please edit the code maybe try a different approach?. – Felipe Franco May 29 '19 at 22:12
  • whereMatches allows you to search in String fields using a Regular Expression. $ is a special char to specify that you want to search in the end of the string. If you search for "2" for example it will match "234", "123" and "012". But if you search for "2$" it will match only "012". Anyway, if you fill more comfortable, you can also use ```query.whereEndsWith("Placa", terminaEn)``` but it is exactly the same thing as you can see here: https://github.com/parse-community/Parse-SDK-Android/blob/29c45f49d0f1aeac4462d2c2a5a29c5b9e599485/parse/src/main/java/com/parse/ParseQuery.java#L1183 – Davi Macêdo May 29 '19 at 22:25
  • 1
    For a best performance, another idea that you can consider is actually create an additional field in your class to store just the last digit of the plate and then use this field to do a regular query that will be much faster and less expensive for the database. – Davi Macêdo May 29 '19 at 22:28
  • yes davi y like the last one better what i did is create another column with the last value! that is a brilliant idea thanks alot ! anyways i will put in the Answer Question part what i did so that people can learn!. – Felipe Franco May 30 '19 at 18:22