{"id":124077,"date":"2026-06-04T10:00:00","date_gmt":"2026-06-04T10:00:00","guid":{"rendered":"https:\/\/foojay.io\/?p=124077"},"modified":"2026-06-03T12:53:19","modified_gmt":"2026-06-03T12:53:19","slug":"mongodb-as-a-vector-database-for-ai-agents-mongodb","status":"publish","type":"post","link":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/","title":{"rendered":"MongoDB as a Vector Database for AI Agents-MongoDB"},"content":{"rendered":"\n    <div class=\"article__table\">\n        <div class=\"article__table-header\">\n            <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                <path d=\"M8 6H21\" stroke=\"#3562E5\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\" \/>\n                <path d=\"M8 12H21\" stroke=\"#3562E5\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\" \/>\n                <path d=\"M8 18H21\" stroke=\"#3562E5\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\" \/>\n                <path d=\"M3 6H3.01\" stroke=\"#3562E5\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\" \/>\n                <path d=\"M3 12H3.01\" stroke=\"#3562E5\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\" \/>\n                <path d=\"M3 18H3.01\" stroke=\"#3562E5\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\" \/>\n            <\/svg>\n            Table of Contents\n            <svg class=\"chevron\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                <path d=\"M18 15L12 9L6 15\" stroke=\"#3562E5\" stroke-width=\"1.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\"\/>\n            <\/svg>\n        <\/div>\n        <div class=\"article__table-body\"><span><a href=\"#h2-0--hy-should-you-use-ongo-for-building-agents-\">Why should you use MongoDB for building AI agents?<\/a><\/span><span><a href=\"#h2-1--nderstanding-agents\">Understanding AI agents<\/a><\/span><span><a href=\"#h2-2--uilding-a-multi-agent-application-with-ongo-\">Building a multi-agent application with MongoDB<\/a><\/span><ul><li><a href=\"#h3-3--tep-1-reating-a-vector-search-index\">Step 1: Creating a vector search index<\/a><\/li><li><a href=\"#h3-4--tep-2-reating-the-rip\">Step 2: Creating the Trip<\/a><\/li><li><a href=\"#h3-5--tep-3-nduce-a-disruption\">Step 3: Induce a disruption<\/a><\/li><li><a href=\"#h3-6--tep-4-eplanning-\">Step 4: Replanning\u00a0<\/a><\/li><li><a href=\"#h3-7--tep-5-he-emory-agents-make-use-of-vector-search-\">Step 5: The Memory agents make use of vector search.<\/a><\/li><li><a href=\"#h3-8--tep-6-ooking-agent-generates-options\">Step 6: Booking agent generates options<\/a><\/li><li><a href=\"#h3-9--tep-7-he-system-finally-makes-the-decision\">Step 7: The system finally makes the decision<\/a><\/li><\/ul><span><a href=\"#h2-10--onclusion\">Conclusion<\/a><\/span><\/div><\/div><!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/REC-html40\/loose.dtd\">\n<?xml encoding=\"utf-8\" ?><html><body><p>Modern artificial intelligence systems are continually evolving. Large Language Models, or LLMs, have become the backbone of modern applications and help build conversational interfaces, like GPS, to more integrated content. However, LLMs lack memory and the capacity to retain content across interactions because they are stateless. And these limitations led to the building of AI agents. These AI agents build beyond simple prompt-response interactions into more autonomous, task-oriented workflows.&nbsp;<\/p>\n\n\n\n<p>These agents are not just model invocations; rather, they are an orchestration layer that combines reasoning with capabilities like retrieval, memory, and tool execution. While developing these agents, a database with the ability to store and retrieve semantically meaningful data is needed, which is where vector databases come into the picture.<\/p>\n\n\n\n<p>A vector database stores data as dense numerical representations of text, images, or unstructured data. These embeddings capture semantic meaning, enabling similarity search instead of exact matching. With <a target=\"_blank\" href=\"https:\/\/www.mongodb.com\/products\/platform\/?utm_campaign=devrel&amp;utm_source=third-party-content&amp;utm_medium=cta&amp;utm_content=vector-mongodb-foojay&amp;utm_term=hugh.murray\">MongoDB Atlas<\/a>, developers can generate embeddings, store them alongside application data, and perform vector search within MongoDB Atlas, thus allowing AI agents to seamlessly combine operational data with semantic retrieval, simplifying architecture while improving performance.<\/p>\n\n\n\n<p>In this blog post, we&rsquo;ll build an AI agent in Java using MongoDB as our database, by storing user queries, documents, agent memory, and embeddings in a single place. We will understand how MongoDB simplifies the implementation of retrieval-augmented generation and persistent memory systems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h2-0--hy-should-you-use-ongo-for-building-agents-\">Why should you use MongoDB for building AI agents?<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Vector store and voyage AI support<\/strong> &ndash; MongoDB Atlas infrastructure offers you a developer-friendly ecosystem. Giving you the ability to store vector embeddings, create vector embeddings, and finally perform the vector search directly from the platform. This reduces the need to have different systems to build an enterprise application.&nbsp;<\/li>\n\n\n\n<li><strong>Hybrid Search &ndash;<\/strong> With MongoDB Atlas infrastructure, you can add filters with a vector search query and add additional conditions to the query results. Unlike specialized vector stores, MongoDB can do both semantic (<a target=\"_blank\" href=\"https:\/\/www.mongodb.com\/products\/platform\/atlas-vector-search\/?utm_campaign=devrel&amp;utm_source=third-party-content&amp;utm_medium=cta&amp;utm_content=vector-mongodb-foojay&amp;utm_term=hugh.murray\">vector<\/a>) and classically structured (keyword) queries together.<\/li>\n\n\n\n<li><strong>Developer Ecosystem &ndash;<\/strong> MongoDB has been a developer-first database ever since, and as it continues to do so, it lets your application integrate efficiently.&nbsp;<\/li>\n\n\n\n<li><strong>Operational Efficiency<\/strong> - If you already use MongoDB, adding vector search avoids the need to introduce new infrastructure. It simplifies schema, transactions, and ops.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h2-1--nderstanding-agents\">Understanding AI agents<\/h2>\n\n\n\n<p>While we are building AI agents, it is important to understand the core principles of embeddings, retrieval-augmented generation (RAG), and agentic architectures.&nbsp;<\/p>\n\n\n\n<p>Vector embeddings, or simply embeddings, are dense vector representations of numerical data derived from texts, audio, videos, or any form of unstructured data. These vectors reside in a high-dimensional space where semantic similarity is preserved, which means semantically similar inputs are located closer together based on distance metrics such as cosine similarity or dot product.<\/p>\n\n\n\n<p>This vector representation helps retrieve the top-K most similar vectors, effectively performing semantic retrieval rather than exact matching using vector search. This is critical for handling paraphrasing, ambiguity, and contextual queries.&nbsp;<\/p>\n\n\n\n<p>With retrieval-augmented generation, or RAG, it builds the retrieval step into a pipeline. The model uses the semantic search ability to generate responses. One of the most common challenges with standard LLMs is hallucination, or the generation of incorrect or fabricated information when relying solely on parametric knowledge stored in model weights. RAG addresses this by grounding responses in retrieved documents rather than depending only on internal weights. As a result, it improves factual consistency, traceability, and the freshness of responses.<\/p>\n\n\n\n<p>With these changes, the concepts of agents came into the picture. In these agentic architectures, vector search becomes a core abstraction for implementing memory systems:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Short-term memory<\/strong>: recent interaction history embedded and retrieved for conversational continuity<\/li>\n\n\n\n<li><strong>Long-term memory<\/strong>: persisted embeddings of past interactions, documents, and tool outputs<\/li>\n\n\n\n<li><strong>Semantic recall<\/strong>: retrieving context dynamically based on similarity rather than rigid keys<\/li>\n<\/ol>\n\n\n\n<p>In these architectures, vector databases serve as both the retrieval and the storage layer for these systems. Therefore, vector search no longer remains just for semantic searches but rather a foundational building block for agentic systems. It underpins how agents retrieve knowledge, maintain memory, and produce contextually relevant, low-hallucination outputs in real-world applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h2-2--uilding-a-multi-agent-application-with-ongo-\">Building a multi-agent application with MongoDB<\/h2>\n\n\n\n<p>Before we get into the actual code for building the agents, let's first understand a few basic prerequisites for building the application.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A free-tier MongoDB Atlas cluster.&nbsp;<\/li>\n\n\n\n<li>Create your free Voyage AI API key to generate embeddings in the database.&nbsp;<\/li>\n\n\n\n<li>A Spring Boot setup to work with MongoDB using Spring Initializr.&nbsp;<\/li>\n\n\n\n<li>Latest Java and Gradle\/Maven versions installed.&nbsp;<\/li>\n<\/ol>\n\n\n\n<p>To build the multi-agent system, we are using a travel replanning system as an example.&nbsp;<\/p>\n\n\n\n<p>Here is a scenario to better understand this system: You are traveling from Toronto to San Francisco with a layover at New York. And then the reality happens. The flight between New York and SF is delayed by 9 hours, and now you need a better plan, since you have that one client meeting to showcase your product.&nbsp;<\/p>\n\n\n\n<p>At this point, we do not need just a system that tells me another way, but rather helps me replan the entire trip. And this is where this multi-agent replanning system would come in. This system basically does the following:&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A Monitoring Agent that detects disruptions<\/li>\n\n\n\n<li>A Planner Agent orchestrates decisions<\/li>\n\n\n\n<li>A Booking Agent finds alternative routes<\/li>\n\n\n\n<li>A Budget Agent filters based on cost<\/li>\n\n\n\n<li>A Preference Agent aligns with user choices<\/li>\n\n\n\n<li>A Memory Agent recalls similar past situations<\/li>\n<\/ul>\n\n\n\n<p>Each agent is simple on its own. But together, they behave like a coordinated system.<\/p>\n\n\n\n<p>What makes this system powerful is the use of MongoDB as the database. MongoDB stores real-time data in a database; every event is recorded in the system, and Voyage AI and MongoDB&rsquo;s vector search capabilities store embeddings of past travel incidents and retrieve similar cases during replanning.<\/p>\n\n\n\n<p>To build this system, we will be using four different collections: <em>trip_state, event, agent_decision, and incident_memory<\/em>. The <em>trip_state<\/em> stores the current state of the trip; all disruptions are copied into <em>events<\/em>. Every agent logs its reasoning in <em>agent_decision,<\/em> and <em>incident_memory<\/em> stores the past incidents.&nbsp;<\/p>\n\n\n\n<p>Let's do this step by step.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h3-3--tep-1-reating-a-vector-search-index\">Step 1: Creating a vector search index<\/h3>\n\n\n\n<p>Before we build the system, we need a vector search index. The embeddings in this project are produced by Voyage AI's<em> voyage-3-large model<\/em>.<\/p>\n\n\n\n<p>Go to MongoDB Atlas, create a collection named <em>incident_memory<\/em>, and create a vector search index with the JSON below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  \"fields\": [\n    {\n      \"numDimensions\": 1024,\n      \"path\": \"embedding\",\n      \"similarity\": \"cosine\",\n      \"type\": \"vector\"\n    }\n  ]\n}\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h3-4--tep-2-reating-the-rip\">Step 2: Creating the Trip<\/h3>\n\n\n\n<p>The trip is created with the following API call. This request lands in the controller. Because the request body is optional, we use a default CreateTripRequest when none is supplied and pass that normalized request into the service. So, normalized is just the incoming request or a default placeholder when the client omits the body.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@PostMapping(\"\/create\")\npublic TripState createTrip(@RequestBody(required = false) CreateTripRequest request) {\n    CreateTripRequest normalized = request == null\n            ? new CreateTripRequest(\"demo-user\", null, null)\n            : request;\n    return tripService.createTrip(normalized);\n}<\/pre>\n\n\n\n<p>And with the Service layer, it creates the trip. Example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl -X POST \"http:\/\/localhost:8080\/trip\/create\" \\\n  -H \"Content-Type: application\/json\" \\\n  -d '{\n    \"userId\": \"traveler-001\",\n    \"preferences\": {\n      \"airlinePreference\": \"SkyJet\",\n      \"avoidRedEye\": true,\n      \"maxAdditionalBudget\": 250\n    }\n  }'<\/pre>\n\n\n\n<p>Would result in:&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  \"id\": \"69dd6111674d2228e4db4b25\",\n  \"userId\": \"traveler-001\",\n  \"itinerary\": [\n    {\n      \"segmentId\": \"SEG-1\",\n      \"type\": \"FLIGHT\",\n      \"provider\": \"SkyJet\",\n      \"fromLocation\": \"JFK\",\n      \"toLocation\": \"SFO\",\n      \"cost\": 420.0\n    }\n  ],\n  \"status\": \"ON_TRACK\"\n}\n<\/pre>\n\n\n\n<p>This trip gets stored in <em>trip_state<\/em>. At this point, everything looks fine.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h3-5--tep-3-nduce-a-disruption\">Step 3: Induce a disruption<\/h3>\n\n\n\n<p>At this step, we would add a delay status in the database. This is done using another post method:&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl -X POST \"http:\/\/localhost:8080\/event\/simulate-delay\" \\\n  -H \"Content-Type: application\/json\" \\\n  -d '{\n    \"tripId\": \"69dd6111674d2228e4db4b25\",\n    \"delayMinutes\": 180,\n    \"severity\": \"HIGH\"\n  }'\n<\/pre>\n\n\n\n<p>This is done using another code block in the controller.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@PostMapping(\"\/simulate-delay\")\npublic TravelEvent simulateDelay(@RequestBody SimulateDelayRequest request)<\/pre>\n\n\n\n<p>And at the same time, something critical happens:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">tripState.setStatus(TripStatus.DISRUPTED);\ntripService.saveTrip(tripState);<\/pre>\n\n\n\n<p>This is your first agent that detects a problem, updates the state, and logs the decision.&nbsp;<\/p>\n\n\n\n<p>The following delay is stimulated:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  \"id\": \"69dd6160674d2228e4db4b26\",\n  \"tripId\": \"69dd6111674d2228e4db4b25\",\n  \"type\": \"FLIGHT_DELAY\",\n  \"severity\": \"HIGH\",\n  \"metadata\": {\n    \"from\": \"JFK\",\n    \"to\": \"SFO\",\n    \"delayMinutes\": 180\n  }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h3-6--tep-4-eplanning-\">Step 4: Replanning&nbsp;<\/h3>\n\n\n\n<p>To trigger replanning, the PlannerAgent orchestrates the other agents. It asks MemoryAgent for similar incidents using MongoDB Vector Search and asks BookingAgent for alternative routes; then BudgetAgent and PreferenceAgent refine those options before PlannerAgent commits the final itinerary.<\/p>\n\n\n\n<p>This enters the<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@PostMapping(\"\/plan\/replan\")\npublic TripState replan(@RequestBody ReplanRequest request)<\/pre>\n\n\n\n<p>And the planner agent takes over. Example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl -X POST http:\/\/localhost:8080\/plan\/replan \\\n  -H \"Content-Type: application\/json\" \\\n  -d '{\n    \"tripId\": \"69dd6111674d2228e4db4b25\"\n  }'<\/pre>\n\n\n\n<p>Which responds as<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  \"id\": \"69dd6111674d2228e4db4b25\",\n  \"status\": \"REPLANNED\",\n  \"itinerary\": [\n    {\n      \"segmentId\": \"OPT-CHI-1\",\n      \"fromLocation\": \"JFK\",\n      \"toLocation\": \"ORD\",\n      \"cost\": 320.0\n    },\n    {\n      \"segmentId\": \"OPT-CHI-2\",\n      \"fromLocation\": \"ORD\",\n      \"toLocation\": \"SFO\",\n      \"cost\": 320.0\n    }\n  ]\n}<\/pre>\n\n\n\n<p>This is where it starts to suggest taking another flight from Chicago.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h3-7--tep-5-he-emory-agents-make-use-of-vector-search-\">Step 5: The Memory agents make use of vector search.<\/h3>\n\n\n\n<p>At first, the planner agents check, \"Have we seen something like this?\" If so, they retrieve it from the <em>incident_memory<\/em> and suggest what could be done.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">List&lt;IncidentMemory&gt; results = vectorSearchService.findSimilar(query);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h3-8--tep-6-ooking-agent-generates-options\">Step 6: Booking agent generates options<\/h3>\n\n\n\n<p>At this point, when no response is found, it starts to generate its own options. To do so,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">List&lt;AlternativeRoute&gt; options =\n    bookingAgent.generateOptions(tripState, latestEvent, memories);<\/pre>\n\n\n\n<p>The budget agent also starts to filter options with&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">List&lt;AlternativeRoute&gt; budgeted =\n    budgetAgent.filterOptions(tripState, options);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h3-9--tep-7-he-system-finally-makes-the-decision\">Step 7: The system finally makes the decision<\/h3>\n\n\n\n<p>Finally, the trip is updated, and the system records the reason for the same. At this point, when you call:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"bash\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl http:\/\/localhost:8080\/trip\/69dd6111674d2228e4db4b25<\/pre>\n\n\n\n<p>It would give you the response as:&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  \"status\": \"REPLANNED\",\n  \"itinerary\": [\n    {\n      \"fromLocation\": \"JFK\",\n      \"toLocation\": \"ORD\"\n    },\n    {\n      \"fromLocation\": \"ORD\",\n      \"toLocation\": \"SFO\"\n    }\n  ]\n}<\/pre>\n\n\n\n<p>Finally, the system didn&rsquo;t just detect a delay, but it used memory, coordinated multiple agents, and produced a better plan with a fully traceable decision history stored in MongoDB.<\/p>\n\n\n\n<p>The complete code for this multi-agent system is available on the <a target=\"_blank\" href=\"https:\/\/github.com\/aasawariS\/travel_multi-agent_with_mongodb\">GitHub repository<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h2-10--onclusion\">Conclusion<\/h2>\n\n\n\n<p>In this blog, we tried to build a multi-agent system that is adaptive, stateful, and intelligent, all using MongoDB.&nbsp;<\/p>\n\n\n\n<p>Starting from a simple travel itinerary, we saw how a disruption triggered a chain of coordinated actions across multiple agents. The Monitoring Agent detected the issue, the Memory Agent recalled similar past incidents using vector search, and the Planner Agent orchestrated Booking, Budget, and Preference Agents to arrive at a better alternative. Most importantly, every step of this process was persisted, making the system not just intelligent, but also explainable.<\/p>\n\n\n\n<p>What makes this architecture powerful is the role of MongoDB as a unified data platform. Instead of separating operational data and AI memory into separate systems, MongoDB brings them together: This allows agents to move beyond stateless execution and operate with context and experience.<\/p>\n\n\n\n<p>The vector search capability of MongoDB enables the system to retrieve similar past situations and apply that knowledge to new problems, reducing guesswork and improving decision quality.<\/p>\n<\/body><\/html>\n","protected":false},"excerpt":{"rendered":"<p>Table of Contents Why should you use MongoDB for building AI agents?Understanding AI agentsBuilding a multi-agent application with MongoDB Step 1: Creating a vector search index Step 2: Creating the Trip Step 3: Induce a disruption Step 4: Replanning\u00a0 Step &#8230;<\/p>\n","protected":false},"author":711,"featured_media":124078,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2269,1],"tags":[2422,439,33,1417,2560,2558],"class_list":["post-124077","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongo","category-uncategorized","tag-ai-agents","tag-database","tag-java","tag-mongodb","tag-retrieval-augmented-generation","tag-vector-search"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MongoDB as a Vector Database for AI Agents-MongoDB<\/title>\n<meta name=\"description\" content=\"Modern artificial intelligence systems are continually evolving. Large Language Models, or LLMs, have become the backbone of modern applications and help build conversational interfaces, like GPS, to more integrated content. However, LLMs lack memory and the capacity to retain content across interactions because they are stateless. And these limitations led to the building of AI agents. These AI agents build beyond simple prompt-response interactions into more autonomous, task-oriented workflows.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB as a Vector Database for AI Agents-MongoDB\" \/>\n<meta property=\"og:description\" content=\"Modern artificial intelligence systems are continually evolving. Large Language Models, or LLMs, have become the backbone of modern applications and help build conversational interfaces, like GPS, to more integrated content. However, LLMs lack memory and the capacity to retain content across interactions because they are stateless. And these limitations led to the building of AI agents. These AI agents build beyond simple prompt-response interactions into more autonomous, task-oriented workflows.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/\" \/>\n<meta property=\"og:site_name\" content=\"foojay\" \/>\n<meta property=\"article:published_time\" content=\"2026-06-04T10:00:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/foojay.io\/wp-content\/uploads\/2026\/06\/Brand-Shape-Yes-Shape-Color-Lavender-7-e1780489356637.png\" \/>\n\t<meta property=\"og:image:width\" content=\"600\" \/>\n\t<meta property=\"og:image:height\" content=\"399\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Aasawari Sahasrabuddhe\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aasawari Sahasrabuddhe\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/\"},\"author\":{\"name\":\"Aasawari Sahasrabuddhe\",\"@id\":\"https:\\\/\\\/foojay.io\\\/#\\\/schema\\\/person\\\/f1e40925b8700e82d466461bb08b0035\"},\"headline\":\"MongoDB as a Vector Database for AI Agents-MongoDB\",\"datePublished\":\"2026-06-04T10:00:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/\"},\"wordCount\":1596,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/foojay.io\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/foojay.io\\\/wp-content\\\/uploads\\\/2026\\\/06\\\/Brand-Shape-Yes-Shape-Color-Lavender-7-e1780489356637.png\",\"keywords\":[\"AI agents\",\"database\",\"Java\",\"mongoDB\",\"Retrieval Augmented Generation\",\"Vector Search\"],\"articleSection\":[\"Mongo\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/\",\"url\":\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/\",\"name\":\"MongoDB as a Vector Database for AI Agents-MongoDB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/foojay.io\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/foojay.io\\\/wp-content\\\/uploads\\\/2026\\\/06\\\/Brand-Shape-Yes-Shape-Color-Lavender-7-e1780489356637.png\",\"datePublished\":\"2026-06-04T10:00:00+00:00\",\"description\":\"Modern artificial intelligence systems are continually evolving. Large Language Models, or LLMs, have become the backbone of modern applications and help build conversational interfaces, like GPS, to more integrated content. However, LLMs lack memory and the capacity to retain content across interactions because they are stateless. And these limitations led to the building of AI agents. These AI agents build beyond simple prompt-response interactions into more autonomous, task-oriented workflows.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/#primaryimage\",\"url\":\"https:\\\/\\\/foojay.io\\\/wp-content\\\/uploads\\\/2026\\\/06\\\/Brand-Shape-Yes-Shape-Color-Lavender-7-e1780489356637.png\",\"contentUrl\":\"https:\\\/\\\/foojay.io\\\/wp-content\\\/uploads\\\/2026\\\/06\\\/Brand-Shape-Yes-Shape-Color-Lavender-7-e1780489356637.png\",\"width\":600,\"height\":399},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/foojay.io\\\/today\\\/mongodb-as-a-vector-database-for-ai-agents-mongodb\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/foojay.io\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MongoDB as a Vector Database for AI Agents-MongoDB\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/foojay.io\\\/#website\",\"url\":\"https:\\\/\\\/foojay.io\\\/\",\"name\":\"foojay\",\"description\":\"a place for friends of OpenJDK\",\"publisher\":{\"@id\":\"https:\\\/\\\/foojay.io\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/foojay.io\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/foojay.io\\\/#organization\",\"name\":\"foojay\",\"url\":\"https:\\\/\\\/foojay.io\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/foojay.io\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/foojay.io\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/cropped-Favicon.png\",\"contentUrl\":\"https:\\\/\\\/foojay.io\\\/wp-content\\\/uploads\\\/2020\\\/04\\\/cropped-Favicon.png\",\"width\":512,\"height\":512,\"caption\":\"foojay\"},\"image\":{\"@id\":\"https:\\\/\\\/foojay.io\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/foojay2020\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/foojay.io\\\/#\\\/schema\\\/person\\\/f1e40925b8700e82d466461bb08b0035\",\"name\":\"Aasawari Sahasrabuddhe\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/foojay.io\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/1707130502328-96x96.jpeg\",\"url\":\"https:\\\/\\\/foojay.io\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/1707130502328-96x96.jpeg\",\"contentUrl\":\"https:\\\/\\\/foojay.io\\\/wp-content\\\/uploads\\\/2025\\\/07\\\/1707130502328-96x96.jpeg\",\"caption\":\"Aasawari Sahasrabuddhe\"},\"description\":\"Senior Software Engineer | Developer Advocate | Java, PHP &amp; MongoDB Specialist | Driving Developer Engagement &amp; Adoption | Workshop Leader &amp; Technical Content Creator\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/aasawaris\\\/\"],\"url\":\"https:\\\/\\\/foojay.io\\\/today\\\/author\\\/aasawari-sahasrabuddhe\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MongoDB as a Vector Database for AI Agents-MongoDB","description":"Modern artificial intelligence systems are continually evolving. Large Language Models, or LLMs, have become the backbone of modern applications and help build conversational interfaces, like GPS, to more integrated content. However, LLMs lack memory and the capacity to retain content across interactions because they are stateless. And these limitations led to the building of AI agents. These AI agents build beyond simple prompt-response interactions into more autonomous, task-oriented workflows.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/","og_locale":"en_US","og_type":"article","og_title":"MongoDB as a Vector Database for AI Agents-MongoDB","og_description":"Modern artificial intelligence systems are continually evolving. Large Language Models, or LLMs, have become the backbone of modern applications and help build conversational interfaces, like GPS, to more integrated content. However, LLMs lack memory and the capacity to retain content across interactions because they are stateless. And these limitations led to the building of AI agents. These AI agents build beyond simple prompt-response interactions into more autonomous, task-oriented workflows.","og_url":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/","og_site_name":"foojay","article_published_time":"2026-06-04T10:00:00+00:00","og_image":[{"width":600,"height":399,"url":"https:\/\/foojay.io\/wp-content\/uploads\/2026\/06\/Brand-Shape-Yes-Shape-Color-Lavender-7-e1780489356637.png","type":"image\/png"}],"author":"Aasawari Sahasrabuddhe","twitter_misc":{"Written by":"Aasawari Sahasrabuddhe","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/#article","isPartOf":{"@id":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/"},"author":{"name":"Aasawari Sahasrabuddhe","@id":"https:\/\/foojay.io\/#\/schema\/person\/f1e40925b8700e82d466461bb08b0035"},"headline":"MongoDB as a Vector Database for AI Agents-MongoDB","datePublished":"2026-06-04T10:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/"},"wordCount":1596,"commentCount":0,"publisher":{"@id":"https:\/\/foojay.io\/#organization"},"image":{"@id":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/#primaryimage"},"thumbnailUrl":"https:\/\/foojay.io\/wp-content\/uploads\/2026\/06\/Brand-Shape-Yes-Shape-Color-Lavender-7-e1780489356637.png","keywords":["AI agents","database","Java","mongoDB","Retrieval Augmented Generation","Vector Search"],"articleSection":["Mongo"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/","url":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/","name":"MongoDB as a Vector Database for AI Agents-MongoDB","isPartOf":{"@id":"https:\/\/foojay.io\/#website"},"primaryImageOfPage":{"@id":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/#primaryimage"},"image":{"@id":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/#primaryimage"},"thumbnailUrl":"https:\/\/foojay.io\/wp-content\/uploads\/2026\/06\/Brand-Shape-Yes-Shape-Color-Lavender-7-e1780489356637.png","datePublished":"2026-06-04T10:00:00+00:00","description":"Modern artificial intelligence systems are continually evolving. Large Language Models, or LLMs, have become the backbone of modern applications and help build conversational interfaces, like GPS, to more integrated content. However, LLMs lack memory and the capacity to retain content across interactions because they are stateless. And these limitations led to the building of AI agents. These AI agents build beyond simple prompt-response interactions into more autonomous, task-oriented workflows.","breadcrumb":{"@id":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/#primaryimage","url":"https:\/\/foojay.io\/wp-content\/uploads\/2026\/06\/Brand-Shape-Yes-Shape-Color-Lavender-7-e1780489356637.png","contentUrl":"https:\/\/foojay.io\/wp-content\/uploads\/2026\/06\/Brand-Shape-Yes-Shape-Color-Lavender-7-e1780489356637.png","width":600,"height":399},{"@type":"BreadcrumbList","@id":"https:\/\/foojay.io\/today\/mongodb-as-a-vector-database-for-ai-agents-mongodb\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/foojay.io\/"},{"@type":"ListItem","position":2,"name":"MongoDB as a Vector Database for AI Agents-MongoDB"}]},{"@type":"WebSite","@id":"https:\/\/foojay.io\/#website","url":"https:\/\/foojay.io\/","name":"foojay","description":"a place for friends of OpenJDK","publisher":{"@id":"https:\/\/foojay.io\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/foojay.io\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/foojay.io\/#organization","name":"foojay","url":"https:\/\/foojay.io\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/foojay.io\/#\/schema\/logo\/image\/","url":"https:\/\/foojay.io\/wp-content\/uploads\/2020\/04\/cropped-Favicon.png","contentUrl":"https:\/\/foojay.io\/wp-content\/uploads\/2020\/04\/cropped-Favicon.png","width":512,"height":512,"caption":"foojay"},"image":{"@id":"https:\/\/foojay.io\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/foojay2020"]},{"@type":"Person","@id":"https:\/\/foojay.io\/#\/schema\/person\/f1e40925b8700e82d466461bb08b0035","name":"Aasawari Sahasrabuddhe","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/foojay.io\/wp-content\/uploads\/2025\/07\/1707130502328-96x96.jpeg","url":"https:\/\/foojay.io\/wp-content\/uploads\/2025\/07\/1707130502328-96x96.jpeg","contentUrl":"https:\/\/foojay.io\/wp-content\/uploads\/2025\/07\/1707130502328-96x96.jpeg","caption":"Aasawari Sahasrabuddhe"},"description":"Senior Software Engineer | Developer Advocate | Java, PHP &amp; MongoDB Specialist | Driving Developer Engagement &amp; Adoption | Workshop Leader &amp; Technical Content Creator","sameAs":["https:\/\/www.linkedin.com\/in\/aasawaris\/"],"url":"https:\/\/foojay.io\/today\/author\/aasawari-sahasrabuddhe\/"}]}},"_links":{"self":[{"href":"https:\/\/foojay.io\/wp-json\/wp\/v2\/posts\/124077","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/foojay.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/foojay.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/foojay.io\/wp-json\/wp\/v2\/users\/711"}],"replies":[{"embeddable":true,"href":"https:\/\/foojay.io\/wp-json\/wp\/v2\/comments?post=124077"}],"version-history":[{"count":0,"href":"https:\/\/foojay.io\/wp-json\/wp\/v2\/posts\/124077\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/foojay.io\/wp-json\/wp\/v2\/media\/124078"}],"wp:attachment":[{"href":"https:\/\/foojay.io\/wp-json\/wp\/v2\/media?parent=124077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/foojay.io\/wp-json\/wp\/v2\/categories?post=124077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/foojay.io\/wp-json\/wp\/v2\/tags?post=124077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}