OdieO
OdieO

Reputation: 6994

Parse.com - Query within an array of objects

I have a Photo Class and a Comments Class. I'm trying to implement a search where a user can retrieve Photos that contain Comments with certain words in them. I've thought of several ways to go about this and the simplest seems to be to include an array of Comments objects in the Photo Class.

However, I'm having trouble figure out how to query this array of comment objects to query their key 'content'.


Doing something like creating an array of the comment strings themselves would work, but I'd rather save a pointer to the comments for reasons of consistency.

Upvotes: 3

Views: 3774

Answers (2)

Pork 'n' Bunny
Pork 'n' Bunny

Reputation: 6731

Timothy is correct, so feel free to vote for whatever (never bad to share the love), I'm just paraphrasing.

You build a query against your comments table and then match it up to your photos.

PFQuery *commentsQuery = [PFQuery queryWithClassName:@"Comments"];

//you can keep entering more or queries to get more terms
[commentsQuery whereKey:@"content containsString:"searchterm"]

PFQuery *photosQuery = [PFQuery queryWithClassName:@"Photos"];
[photosQuery whereKey:@"_id" matchesKey:@"photo" inQuery:commentsQuery]
[photosQuery fetch];

Upvotes: 8

Timothy Walters
Timothy Walters

Reputation: 16874

The help documentation on Relational Queries is what you will want to have a look through.

Basically you create a query for your comments array/relation, and then use the photosQuery.matchesQuery("comments", commentsQuery) call to limit photos to those where the comments match your sub-query.

Upvotes: 2

Related Questions