Multi-Dimensional Querying - JavaScript SDK

By Zach Case

Note: This article is a continuation of my other article, Pointer/Relations in JavaScript SDK. If you do not know what Pointers are when it comes to Parse, or you do not understand them, I highly suggest you go read that post first, and then read this.

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("", "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.

```javascript // 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.

```javascript // 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!


Pointer Relation article

matchQuery() documentation

GitHub CoderWall Repo

See More Posts About:


Posted by Zach Case

LinkedIn Website