Last Updated: February 25, 2016
·
2.708K
· zachcase

Multi-Dimensional Querying - Parse.com JavaScript SDK

Alright, so let me clear the air at the start. This really isn't "Multi-dimensional" in the sense that you can use one query line of code to get your desired results, but it is a way you can query a Pointer relation in your Class with data from the other Class the Pointer is pointing to.


Our Problem

@tyaramis brought up a rather good question on how he could query for all of the posts in his "Post" class, but restrict it so that it only returned posts if the Author User's country was a certain place. In his Post class he has a column set to be a Pointer that points to the _User class. However, when he tried to use the standard query equalTo() method and the dot notation I covered in my Pointer Relation article it returned an error. So he was curious how he could achieve his goal.

Example of Wrong Query
query.equalTo("user.country", "USA"); // Returns error


Our Solution

Parse has a rather nice built in function to allow for this kind of functionality that @tyaramis needs. If we read the Parse JavaScript SDK reference documents we can find a method for our Parse query called, matchQuery(). This method allows us to provide it with a query to match what is inside the pointer.

So, in order to use this with our problem we need to first set up our queries. In this article, I am going to provide the standard way to get all the posts and how to get the posts from users in a specific country.

// Set Up Queries to Grab Posts
var allQuery = new Parse.Query("Post");
var usaQuery = new Parse.Query("Post");

// Set Up Query to Find Specific Posts
var usaPosts = new Parse.Query("_User");
usaPosts.equalTo('country', 'USA');

The first two queries are to get the posts, we need two so that we can have one query that grabs ALL of the posts, and one query that we can provide with restrictions in order to get posts where the author is from a specific country.

Now all that is left to do to get all the posts is to run the query allQuery and do whatever we need to with the post data returned. And to get posts for the specific country, all we have left is to provide the query with the matchQuery() method, and then run it and process the data how we desire.

// Get all The Posts
allQuery.find().then(function(data){
    /* ... Now do whatever is desired with the post data */
});

// Get only USA Posts
usaQuery.matchesQuery('user', usaPosts);
usaQuery.find().then(function(data){
    /* ... Now do whatever is desired with the post data, however the post data will only be from posts where the author's country is USA */
});

Yay, now we have a way to restrict the posts that are returned to us by which country the author user is from.

Please let me know if you have any questions, and as always, this article's source code is on my GitHub. For this article's source code I used States instead of Countries.

Cheers, Zach!


Links:

Pointer Relation article: https://coderwall.com/p/cy6cea

matchQuery() documentation: https://www.parse.com/docs/js/symbols/Parse.Query.html#matchesQuery

GitHub CoderWall Repo: https://github.com/ZachCase/CoderWall

4 Responses
Add your response

Thanks a lot for your feedback Zach..
It works..

Cheers
Tolga

over 1 year ago ·

Glad it worked for ya bud!

Cheers!

over 1 year ago ·

In your example, Post has a pointer to _User. Can you show an example where something has a pointer to Posts instead? Maybe a "Favorited" or a "Flagged" class, where you store both a pointer to the Post and a _User, and want to query for Posts that have (or haven't) been Favorited (or Flagged)?

over 1 year ago ·

Thanks for the help! I've got to say though I'm still struggling with this one. Mind if you take a look at my problem? http://stackoverflow.com/questions/35213216/restrict-query-to-get-all-items-with-specific-pointer-javascript-parse-com/35242687#35242687

I'm trying to restrict my query to retrieve only the posts from a certain author. A blog post has an author, which is a Pointer: (_User).

over 1 year ago ·