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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How to Write An iOS App that Uses a Node.js/MongoDB Web Service
40 mins
- Getting Started
- Setting up Your Node.js Instance
- The Data Model of Your App
- Loading Locations from the Server
- Saving Locations to the Server
- Saving Images to the Server
- Saving Images in your App
- A Quick Recap of File Handling
- Testing it Out
- Querying for Locations
- Using Queries to Filter by Category
- Where to Go From Here?
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:
- This is the list of selected categories.
- The
$in
operator accepts a MongoDB document if the specified propertycategories
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:
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!