BlogAnnounced at MongoDB.local NYC 2024: A recap of all announcements and updatesLearn more >>
MongoDB Developer
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right

Using MongoDB Atlas Triggers to Summarize Airbnb Reviews with OpenAI

Pavel Duchovny4 min read • Published Oct 31, 2023 • Updated Oct 31, 2023
AINode.jsChange StreamsJavaScriptMongoDB
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
In the realm of property rentals, reviews play a pivotal role. MongoDB Atlas triggers, combined with the power of OpenAI's models, can help summarize and analyze these reviews in real-time. In this article, we'll explore how to utilize MongoDB Atlas triggers to process Airbnb reviews, yielding concise summaries and relevant tags.
AI review UI and tags.
This article is an additional feature added to the hotels and apartment sentiment search application developed in Leveraging OpenAI and MongoDB Atlas for Improved Search Functionality.


MongoDB Atlas triggers allow users to define functions that execute in real-time in response to database operations. These triggers can be harnessed to enhance data processing and analysis capabilities. In this example, we aim to generate summarized reviews and tags for a sample Airbnb dataset.
Our original data model has each review embedded in the listing document as an array:


  • App Services application (e.g., application-0). Ensure linkage to the cluster with the Airbnb data.
  • OpenAI account with API access.
Open AI Key Open AI Key

Secrets and Values

  1. Navigate to your App Services application.
  2. Under "Values," create a secret named openAIKey with your OPEN AI API key. Secrets and Values
  3. Create a linked value named OpenAIKey and link to the secret.

The trigger code

The provided trigger listens for changes in the sample_airbnb.listingsAndReviews collection. Upon detecting a new review, it samples up to 50 reviews, sends them to OpenAI's API for summarization, and updates the original document with the summarized content and tags. Atlas Trigger Configuration
Please notice that the trigger reacts to updates that were marked with "process" : false flag. This field indicates that there were no summary created for this batch of reviews yet.
Example of a review update operation that will fire this trigger:

Sample reviews function

To prevent overloading the API with a large number of reviews, a function sampleReviews is defined to randomly sample up to 50 reviews:

Main trigger logic

The main trigger logic is invoked when an update change event is detected with a "process" : false field.
Key steps include:
  • API request preparation: Reviews from the changed document are sampled and prepared into a request string for the OpenAI API. The format and instructions are tailored to ensure the API returns a valid JSON with summarized content and tags.
  • API interaction: Using the method, the trigger sends the prepared data to the OpenAI API.
  • Updating the original document: Upon a successful response from the API, the trigger updates the original document with the summarized content, negative tags (neg_tags), positive tags (pos_tags), and a process flag set to true.
Here is a sample result that is added to the processed listing document:
Once the data is added to our documents, providing this information in our VUE application is as simple as adding this HTML template:


By integrating MongoDB Atlas triggers with OpenAI's powerful models, we can efficiently process and analyze large volumes of reviews in real-time. This setup not only provides concise summaries of reviews but also categorizes them into positive and negative tags, offering valuable insights to property hosts and potential renters.
Questions? Comments? Let’s continue the conversation over in our community forums.

Facebook Icontwitter iconlinkedin icon
Rate this tutorial

Interact with MongoDB Atlas in an AWS Lambda Function Using C#

Jan 23, 2024 | 5 min read

Java - Aggregation Pipeline

Mar 01, 2024 | 8 min read

8 Best Practices for Building FastAPI and MongoDB Applications

Apr 23, 2024 | 8 min read

Create a Java REST API with Quarkus and Eclipse JNoSQL for MongoDB

Jan 30, 2024 | 6 min read
Table of Contents
  • Introduction