Intelligent MongoDB Movie Recommendation Agent - n8n Workflow

Build an intelligent n8n workflow using an AI agent to dynamically query MongoDB for movie recommendations using aggregation pipelines, integrating the OpenAI n8n node for advanced logic.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Developers and engineers using MongoDB for application data.
Data scientists interested in connecting large datasets to intelligent AI agents.
n8n users looking for advanced examples of LangChain Agent implementation.
Anyone needing an intelligent system that executes dynamic database queries based on conversational input.

Overview

This advanced n8n workflow establishes a sophisticated conversational AI agent capable of interacting with a MongoDB database. Unlike static queries, this solution leverages the intelligence of the OpenAI n8n node (acting as the Language Model) to dynamically generate and execute complex MongoDB aggregation pipelines.

The core value of this n8n template lies in its ability to understand natural language requests (e.g., 'Find me highly rated Western movies from the 1950s') and autonomously translate that into a functional $aggregate query via the specialized MongoDBAggregate n8n node. Furthermore, the agent is equipped with a second tool—an insertFavorite workflow tool—allowing users to store confirmed movie preferences back into the database, completing the feedback loop. This powerful application demonstrates the seamless integration of LLMs, proprietary databases, and core n8n flow control.

How it Works

This automation begins instantly upon an external command via the When chat message received n8n trigger. This interactive n8n trigger passes the user's conversational input directly to the core of the system, the AI Agent - Movie Recommendation.


  1. Trigger and Input: The When chat message received n8n trigger (a webhook listener) receives a message (e.g., a recommendation request or a request to save a favorite).

  2. Agent Orchestration: The AI Agent - Movie Recommendation n8n node processes the input. It uses the OpenAI Chat Model n8n node as its brain and maintains conversational history via the Window Buffer Memory.

  3. Tool Selection (Query): If the user asks for a recommendation, the agent determines that the MongoDBAggregate tool is needed. It then instructs the AI to generate a precise MongoDB aggregation pipeline query based on the database schema provided in the tool description.

  4. Database Action: The MongoDBAggregate n8n node executes the dynamically generated pipeline against the movies collection in MongoDB, fetching relevant context.

  5. Tool Selection (Insert): If the user confirms a movie as a favorite, the agent selects the insertFavorite n8n node (a workflow tool) and structures the appropriate JSON payload for insertion.

  6. Response Generation: The agent synthesizes the results from the tools (or confirms the favorite insertion) and generates a final, helpful conversational response back to the user.

Installation Guide

To deploy this comprehensive n8n workflow, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON data and paste it into your n8n canvas using the 'New' -> 'Import from JSON' option.

  2. Set up Credentials:

OpenAI: Click on the OpenAI Chat Model n8n node and set up your OpenAI API Key credential. This is essential for the language model functionality.
MongoDB: Click on the MongoDBAggregate n8n node and configure your MongoDB credential, ensuring it points to the database instance containing your 'movies' collection.

  1. Configure Workflow Tool: The insertFavorite n8n node is configured as a Workflow Tool. You must ensure the target workflow ID (6QuKnOrpusQVu66Q in the example) points to a separate working n8n template designed to handle the MongoDB insertion operation.

  2. Activate and Test: Activate the main n8n workflow. The When chat message received n8n trigger will provide a public webhook URL which can be used to initiate interactions.

Node Details

The following specialized n8n node components drive this AI automation:

When chat message received (n8n trigger): Acts as the entry point, listening for incoming chat messages via a public webhook. This powerful n8n trigger facilitates real-time, conversational interaction.

AI Agent - Movie Recommendation (LangChain Agent n8n node): The orchestrator. It uses a defined prompt to guide its behavior, specifying its ability to query context using MongoDBAggregate and save favorites using insertFavorite.

OpenAI Chat Model (LangChain n8n node): Provides the necessary reasoning power for the Agent, allowing it to interpret requests and generate appropriate MongoDB queries.

Window Buffer Memory (LangChain n8n node): Essential for maintaining conversational context across multiple interactions, enabling the AI agent to remember previous parts of the discussion.

MongoDBAggregate (MongoDB Tool n8n node): This highly customized n8n node allows the Agent to use the $fromAI expression to dynamically generate the MongoDB aggregation pipeline. It provides the AI with the database document schema for context, ensuring accurate query construction.

insertFavorite (Workflow Tool n8n node): This n8n node represents an external workflow call. The AI Agent uses this tool exclusively when the user confirms a favorite movie title, delegating the insertion task to a secondary, specialized n8n workflow.

Related n8n Workflows

Free

Nodes: 7 Nodes
Updated: December 26 2025
View all
Created by
Pavel Duchovny
Pavel Duchovny

Developer imagination == Innovation ✓ Experienced DBA & DevOps engineer & Web Developer. Develop and deploy automation of servers, infrastructure and security to the cloud. Scaling, upgrading and designing Big scaled systems and databases . Specializing in designing and building big data solutions in both RDBMS and NoSql echosystems. Hardworking and innovative personality.

Featured*