Translate

Thursday 18 October 2018

How to Enrich Data with MongoDB Stitch

This article was originally published on MongoDB. Thank you for supporting the partners who make SitePoint possible.

Here is what we are going to achieve in this tutorial:

Firstly, we are going to write a document to MongoDB using MongoDB Stitch.

The result in our MongoDB collection will look like this:

{
"_id": ObjectId("5bb27712dced5f37bebf388c"),
"Title":"Guardians of the Galaxy"
}

Secondly, a trigger will catch this new insertion and start a function.

Lastly, this function will call the OMDB external API with the given movie title, fetch data about that movie, and finally enrich our MongoDB document with the data we gathered from this API.

This is the final result we expect in our MongoDB collection:

{  
   "_id": ObjectId("5bb27712dced5f37bebf388c"),
   "Title":"Guardians of the Galaxy",
   "Year":"2014",
   "Rated":"PG-13",
   "Released":"01 Aug 2014",
   "Runtime":"121 min",
   "Genre":"Action, Adventure, Comedy",
   "Director":"James Gunn",
   "Writer":"James Gunn, Nicole Perlman, Dan Abnett (based on the Marvel comics by), Andy Lanning (based on the Marvel comics by), Bill Mantlo (character created by: Rocket Raccoon), Keith Giffen (character created by: Rocket Raccoon), Jim Starlin (characters created by: Drax the Destroyer,  Gamora & Thanos), Steve Englehart (character created by: Star-Lord), Steve Gan (character created by: Star-Lord), Steve Gerber (character created by: Howard the Duck), Val Mayerik (character created by: Howard the Duck)",
   "Actors":"Chris Pratt, Zoe Saldana, Dave Bautista, Vin Diesel",
   "Plot":"A group of intergalactic criminals are forced to work together to stop a fanatical warrior from taking control of the universe.",
   "Language":"English",
   "Country":"USA",
   "Awards":"Nominated for 2 Oscars. Another 52 wins & 99 nominations.",
   "Poster":"https://m.media-amazon.com/images/M/MV5BMTAwMjU5OTgxNjZeQTJeQWpwZ15BbWU4MDUxNDYxODEx._V1_SX300.jpg",
   "Ratings":[  
      {  
         "Source":"Internet Movie Database",
         "Value":"8.1/10"
      },
      {  
         "Source":"Rotten Tomatoes",
         "Value":"91%"
      },
      {  
         "Source":"Metacritic",
         "Value":"76/100"
      }
   ],
   "Metascore":"76",
   "imdbRating":"8.1",
   "imdbVotes":"871,949",
   "imdbID":"tt2015381",
   "Type":"movie",
   "DVD":"09 Dec 2014",
   "BoxOffice":"$270,592,504",
   "Production":"Walt Disney Pictures",
   "Website":"http://marvel.com/guardians",
   "Response":"True"
}

Prerequisites

So first of all, if you want to try this at home, it is very easy. The only requirement here is to create a free MongoDB Atlas cluster. This video will show you the steps.

MongoDB Stitch is our serverless platform, built by MongoDB on top of MongoDB Atlas. Once our MongoDB Atlas cluster is ready to use, link a MongoDB Stitch application to it:

  • Click on the left panel on “Stitch Apps”,

  • Then click on “Create New Application”,

  • Pick the name you want for your application,

  • Link it to your free MongoDB Atlas cluster.

Actions

To be able to send a document to MongoDB, we are going to use an HTTP POST service.

  • On the left panel, click on “Services”,
  • Then click on “Add a Service”,
  • Choose a service name “IMDB”,

Note: “IMDB” will be reuse later in the function code. If you choose another name, please make sure to update the code accordingly.

  • Click on “Add Service”,

  • Click on “Add Incoming Webhook”,

  • And copy the screenshot below.

When this is done, hit the “Save” button and you will be in the “Function Editor” screen.

Enter the following code:

exports = function(payload, response) {
  const mongodb = context.services.get("mongodb-atlas");
  const movies = mongodb.db("stitch").collection("movies");
  var body = EJSON.parse(payload.body.text());
  movies.insertOne(body)
  .then(result => {
    response.setStatusCode(201);
  });
};

Click the “Save” button again.

Now that our Service is ready, we can test it!

Go to the “Settings” and you will find your Webhook URL. You can now send an HTTP POST request like this to MongoDB Stitch:

curl -H "Content-Type: application/json" -d '{"Title":"Guardians of the Galaxy"}' https://webhooks.mongodb-stitch.com/api/client/v2.0/app/stitchtapp-abcde/service/IMDB/incoming_webhook/post_movie_title?secret=test

Note: I used a curl command but feel free to use Postman or whatever you are used to.

We can check it worked by having a look at the content of the “stitch.movies” collection in our MongoDB Atlas Cluster:

Now that we can insert a new document into MongoDB Atlas using Stitch, we are going to create the trigger.

The post How to Enrich Data with MongoDB Stitch appeared first on SitePoint.



No comments:

Post a Comment