Search code examples
iosparse-platformpfquery

Parse query on a single string field, where the value matches any string provided in array


I need to write a query that will return all users whose usernames exist in a provided array.

I know you can use [query whereKey:@"arrayColumn" equalTo: @"singleValue"] to return results where a single value exists in an array, but I'm looking for the exact opposite - return results where any items in the array are equal to the value for the provided key. Surprisingly, I didn't see this mentioned in the documentation nor do any of the whereKey options look appropriate.

Here is what I have attempted, but of course, this crashes with error: Cannot do a comparison query for type: __NSArrayI

PFQuery *q = [PFUser query];
[q whereKey:@"username" equalTo:@[@"username1", @"username2"]];
NSArray *a = [q findObjects];
//I need a to contain users with usernames 'username1' and 'username2'

Solution

  • Found the answer. It's really quite simple, I was overthinking.

    Use containedIn.

    PFQuery *q = [PFUser query];
    [q whereKey:@"username" containedIn:@[@"username1", @"username2"]];
    NSArray *a = [q findObjects];