search
HomeTechnology peripheralsIt IndustryHow to Enrich Data with MongoDB Stitch

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:

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);
  });
};

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

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

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:
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
      });
};

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
Top 21 Developer Newsletters to Subscribe To in 2025Top 21 Developer Newsletters to Subscribe To in 2025Apr 24, 2025 am 08:28 AM

Stay informed about the latest tech trends with these top developer newsletters! This curated list offers something for everyone, from AI enthusiasts to seasoned backend and frontend developers. Choose your favorites and save time searching for rel

Serverless Image Processing Pipeline with AWS ECS and LambdaServerless Image Processing Pipeline with AWS ECS and LambdaApr 18, 2025 am 08:28 AM

This tutorial guides you through building a serverless image processing pipeline using AWS services. We'll create a Next.js frontend deployed on an ECS Fargate cluster, interacting with an API Gateway, Lambda functions, S3 buckets, and DynamoDB. Th

CNCF Arm64 Pilot: Impact and InsightsCNCF Arm64 Pilot: Impact and InsightsApr 15, 2025 am 08:27 AM

This pilot program, a collaboration between the CNCF (Cloud Native Computing Foundation), Ampere Computing, Equinix Metal, and Actuated, streamlines arm64 CI/CD for CNCF GitHub projects. The initiative addresses security concerns and performance lim

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.