Yurets
Yurets

Reputation: 4009

How to set OR condition for keys in a query to parse.com in android

I have a table of users in parse.com where each of them contains fields first_name and last_name. I'm doing user search and cannot figure out how to make condition OR. My example:

ParseRelation<ParseUser> relation = user.getRelation("Friends");
ParseQuery<ParseUser> query = relation.getQuery();
query.whereContains("first_name", "typed text"); //need to add OR for the same condition, 
//but for the field "last_name"
query.findInBackground(new FindCallback<ParseUser>() {
    @Override
    public void done(List<ParseUser> objects, ParseException e) {
        if(e == null){
            //doing work
        }else {
            e.printStackTrace();
        }
    }
});

I tried to do this way:

query.whereContains("first_name", "typed text").whereContains("last_name", "typed text");

but no, it's searching for satisfying both conditions. Method whereContainsAll() is applicable for list of values, but not keys.

I can do it by sending 2 requests, but this is horrible solution. Is there a way we can do that?

Upvotes: 1

Views: 2004

Answers (2)

Norutan
Norutan

Reputation: 1520

You can use ParseQuery.or(queries) Here is the docs link: https://parse.com/docs/android/guide#queries-compound-queries

Updated 08-2022:

https://docs.parseplatform.org/android/guide/#compound-queries

Example code from doc:

ParseQuery<ParseObject> lotsOfWins = ParseQuery.getQuery("Player");
lotsOfWins.whereGreaterThan(150);

ParseQuery<ParseObject> fewWins = ParseQuery.getQuery("Player");
fewWins.whereLessThan(5);

List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
queries.add(lotsOfWins);
queries.add(fewWins);

ParseQuery<ParseObject> mainQuery = ParseQuery.or(queries);
mainQuery.findInBackground(new FindCallback<ParseObject>() {
  public void done(List<ParseObject> results, ParseException e) {
    // results has the list of players that win a lot or haven't won much.
  }
});

Upvotes: 3

Anirudh
Anirudh

Reputation: 3418

        ParseQuery<ParseObject> query1 = new ParseQuery<ParseObject>("Place");
        query1.whereEqualTo("email","[email protected]");

        ParseQuery<ParseObject> query2 = new ParseQuery<ParseObject>("Place");
        query2.whereEqualTo("place_mobile",909090XXXX);

        List<ParseQuery<ParseObject>> list = new ArrayList<ParseQuery<ParseObject>>();
        list.add(query1);
        list.add(query2);

        ParseQuery<ParseObject> query = ParseQuery.or(list);
        query.findInBackground();

Upvotes: 0

Related Questions