Home >Technology peripherals >It Industry >How to Enrich Data with MongoDB Stitch

How to Enrich Data with MongoDB Stitch

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2025-02-15 10:35:12780browse

This tutorial demonstrates enriching MongoDB documents with data from an external API using MongoDB Stitch. We'll add movie details from the OMDB API to a MongoDB document after initial insertion.

How to Enrich Data with MongoDB Stitch

Goal: This tutorial shows how to:

  1. Insert a document into MongoDB using a MongoDB Stitch HTTP POST service. The initial document will contain only an _id and a Title.
  2. Create a Stitch trigger that activates upon new document insertion.
  3. Use the trigger to call the OMDB API with the movie title.
  4. Update the original MongoDB document with the fetched movie details.

Prerequisites:

You'll need a free MongoDB Atlas cluster. A video tutorial outlining the setup process is available (link presumably provided in the original). Then, link a MongoDB Stitch application to your Atlas cluster:

  1. Navigate to "Stitch Apps" in the left panel.
  2. Click "Create New Application."
  3. Name your application.
  4. Link it to your MongoDB Atlas cluster.

How to Enrich Data with MongoDB Stitch

Setting up the HTTP POST Service:

  1. In the left panel, go to "Services," then "Add a Service."
  2. Name the service "IMDB" (or choose another name; remember to update the code accordingly).
  3. Add an Incoming Webhook and note the following configuration (screenshot provided in original).

How to Enrich Data with MongoDB Stitch

The following function code will handle the initial document insertion:

<code class="language-javascript">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);
  });
};</code>

Save the function. Test it using a curl command (or Postman) like this, replacing the placeholder URL and secret:

<code class="language-bash">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</code>

Verify the insertion in your MongoDB Atlas cluster.

How to Enrich Data with MongoDB Stitch

Creating the Trigger and Enrichment Function:

  1. In the left panel, go to "Triggers," then "Add a Database Trigger."
  2. Configure the trigger as shown in the original (screenshot provided).
  3. Use the following function code to fetch and add movie details from the OMDB API:
<code class="language-javascript">exports = function(changeEvent) {
  var docId = changeEvent.documentKey._id;
  var title = encodeURIComponent(changeEvent.fullDocument.Title.trim());

  var movies = context.services.get("mongodb-atlas").db("stitch").collection("movies");
  var imdb_url = "http://www.omdbapi.com/?apikey=[YOUR_OMDB_API_KEY]&t=" + title;

  const http = context.services.get("IMDB");
    return http
      .get({ url: imdb_url })
      .then(resp => {
        var doc = EJSON.parse(resp.body.text());
        movies.updateOne({"_id":docId}, {$set: doc}); // Use $set to update only the new fields
      });
};</code>

Remember to replace [YOUR_OMDB_API_KEY] with your actual OMDB API key (obtain one from https://www.php.cn/link/fcf70ea0bbeb4edca72cc304e75f4c98). The $set operator is used to prevent overwriting existing fields.

Test the trigger by sending another curl request. The updated document should now contain the enriched movie data.

How to Enrich Data with MongoDB Stitch

Summary:

This process demonstrates a powerful way to integrate external APIs with your MongoDB data using MongoDB Stitch's serverless capabilities. The event-driven architecture allows for efficient data enrichment without complex server-side logic.

Further Reading:

  • MongoDB Stitch Billing: (link presumably provided in the original)
  • Querying MongoDB Atlas with MongoDB Stitch: (link presumably provided in the original)

The above is the detailed content of How to Enrich Data with MongoDB Stitch. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn