InfinitePrime
InfinitePrime

Reputation: 1776

Comparing two arrays in parse query

How do we find if any element of an array is part of another array in a query?

var followers = []; // Array of Parse User pointers query.howTo("attending", followers); // attending is an array of User Pointers.

That is, the query should match if any one or more of the elements in followers exists in attending.

query.containsAll matches for all the elements. Is there something like query.containsSome ?

Upvotes: 1

Views: 770

Answers (1)

Natan R.
Natan R.

Reputation: 5181

I was pretty sure you can query two arrays. Take a look into the docs to check better.

In case it doesn't, you can use compound queries.

For example, generate an array of queries, based on the array of followers. The [forEach] is a better idea in this case, but I'm supposing here a for loop.

var followers = []; //array of users var mainQuery = new Parse.Query(YourOtherObject); //for each one of followers var orQuery = new Parse.Query(YourOtherObject); orQuery.equalTo("attending", follower); mainQuery = Parse.Query.or(mainQuery, orQuery);

This solution might not be performant if your followers areay is too big. But in any case, I still recommend using relations in this case, as you benefit from the inverse, and can get from the users query, where he is present as attending in the other Object.

Upvotes: 1

Related Questions