Connecting MongoDB Stitch to Google Places

Facebook ShareLinkedin ShareReddit ShareTwitter Share

One of the services that make available a wealth of data via API, is Google Places.

Imagine we want to provide users of our application with information about a business with whom we partner. Insurance companies do this with providers of accommodations, transportation, and healthcare. We don’t want to maintain this information, or own it - rather, we’d prefer to leverage a service that provides this information about these service providers. Google Places is just such a service provider.

For this application, we’ll use the following Stitch components to integrate MongoDB with Google Places.

Stitch Functions

Stitch functions are written in JavaScript ES6 and can be called from our SDKs, Triggers, or Webhooks and are great for coordinating data access or doing light processing alongside a query or insert. Communicating with data provider services such as Google Places is as simple as leveraging an HTTP service within a serverless stitch function:

const http ="GooglePlaces");
 return http
   .get({url: GooglePlacesSearchURL})
       //The response body is encoded as raw BSON.Binary. Parse it to JSON.
       var search_result = EJSON.parse(resp.body.text());

Stitch’s Functions also let you reference context – such as services, variables, or user information – in order to make it easier to leverage services and information across your entire application. Stitch also provides several third-party services including AWS, Twilio, and Github.

Stitch Services

The HTTP service that we create here will also have an incoming webhook, meaning that it can make outgoing HTTP requests within Stitch Functions, but also handle incoming HTTP services.

Stitch Trigger

Stitch Triggers enable reactivity to inserts, updates, deletes, and replaces that occur in the database. In our case, an insert will trigger execution of a function.

Figure 1. Trigger Configuration

Building Your Application

Let’s take a look at how all the pieces of this application fit together –

Figure 2. Stitch Architectural Diagram
  1. In step 1, an application accepts input either from a user or as a result of some action the user performed (using geofencing, for example.) The input, in our case, will be the name of a business. The application will insert a document with the name of the business into MongoDB.
  2. The firing of the trigger is automatic because we configured it to watch for inserts or updates to our database.
  3. The trigger executes a custom function called getGooglePlaceInfo then captures and forwards the entire inserted document.
  4. Next, in step 4, the function we created invokes the HTTP Webhook we created. The webhook conducts the conversation between Google Places and Stitch.
  5. In step 5, Google Places will respond with a JSON document containing the requested information.

The function will catch this JSON information and update the MongoDB document. It is worth saying that the function can also manipulate the data before inserting it. Allowing it meet all your project requirements (format, types, calculations). As an example, the function may create a new GeoJSON object from the Google coordinates. All of this is done in step 6.

In Conclusion

We’ve taken a very brief look at how leveraging MongoDB Atlas, Stitch, and Triggers in conjunction with a data API service such as Google Places transforms applications into intelligent apps users will truly love to use. Because we’re adding data without having to bother the user, the application becomes much more usable, much more valuable. MongoDB Stitch and Triggers give your application the ability to react to changes in the database. Then leverage integration with external services to fetch in-context data to enrich your applications’ data further. This improves both usability and value to the user.

Without MongoDB Stitch, a developer would have had to contend with building an application server, dealing with management, availability, scalability, and backup and restoration of the data.

Oh, and did we mention that Stitch provides other benefits as well? It leverages Atlas security, adds third-party authentication and granular, field-level access controls to MongoDB data. This gives the ability for users to retrieve data anywhere. Without developers having to create REST APIs from scratch, secure and maintain them?

The content described in this blog article is publically available and can be found here: