How to Write An iOS App that Uses a Node.js/MongoDB Web Service

Learn how to write an iOS app that uses Node.js and MongoDB for its back end. By Michael Katz.

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Using Queries to Filter by Category

The last bit to add categories to your Locations that users can filter on. This filtering can re-use the server work done in the previous section through the use of MongoDB’s array conditional operators.

Replace the stubbed-out implementation of query in Categories.m with the following code:

+ (NSString*) query
{
    NSArray* a = [self filteredCategories:YES]; //1
    NSString* query = @"";
    if (a.count > 0) {
        
        query = [NSString stringWithFormat:@"{\"categories\":{\"$in\":[%@]}}", [a componentsJoinedByString:@","]]; //2
        query = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                           (CFStringRef) query,
                                                                                           NULL,
                                                                                           (CFStringRef) @"!*();':@&=+$,/?%#[]{}",
                                                                                           kCFStringEncodingUTF8));
       
        query = [@"?query=" stringByAppendingString:query];
    }
    return query;
}

This creates a query string similar to the one used by the geolocation query that has the following differences:

  1. This is the list of selected categories.
  2. The $in operator accepts a MongoDB document if the specified property categories has a value matching any of the items in the corresponding array.

Build and run your app; add a few Locations and assign them one or more categories. Tap the folder icon and select a category to filter on. The map will reload just the Location annotations matching the selected categories as shown below:

A map with many locations

A map with many locations

Select just the "Park" category

Select just the "Park" category

Map after filtering

Map after filtering

Where to Go From Here?

You can download the completed sample project here.

In this tutorial you covered the basics of MongoDB storage — but there's a ton of functionality beyond what you covered here.

MongoDB offers a multitude of options for selecting data out of the database; as well there are a host of server-side features to manage scaling and security. As well, your Node.js installation could definitely be improved by adding user authentication and more privacy around the data.

As for your iOS app, you could add a pile of interesting features, including the following:

  • Routing users to points of interest
  • Adding additional media to locations
  • Improved text editing

Additionally, every decent networked app should cache data locally so it remains functional when data connections are spotty.

Hopefully you've enjoyed this small taste of Node.js, Express and MongoDB — if you have any questions or comments please come join the discussion below!