<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/introduction-to-retrieval-augmented-generation-with-java-and-mongodb-jnxie1wns" -->

---
title: Introduction to Retrieval-Augmented Generation with Java...
description: A step-by-step tutorial for building a Retrieval-Augmented Generation (RAG) application in Java using Jakarta EE, LangChain4j, and MongoDB Atlas. The guide...
canonical: https://daily.dev/posts/introduction-to-retrieval-augmented-generation-with-java-and-mongodb-jnxie1wns
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Introduction to Retrieval-Augmented Generation with Java and MongoDB | daily.dev
og:description: A step-by-step tutorial for building a Retrieval-Augmented Generation (RAG) application in Java using Jakarta EE, LangChain4j, and MongoDB Atlas. The guide...
og:url: https://daily.dev/posts/introduction-to-retrieval-augmented-generation-with-java-and-mongodb-jnxie1wns
og:image: https://api.daily.dev/og/posts/jNxIe1wNS.png
og:image:alt: Introduction to Retrieval-Augmented Generation with Java and MongoDB
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Introduction to Retrieval-Augmented Generation with Java and MongoDB

**[Foojay.io](https://daily.dev/sources/foojayio)** · 11 min read · 0 upvotes · 0 comments

## Summary

A step-by-step tutorial for building a Retrieval-Augmented Generation (RAG) application in Java using Jakarta EE, LangChain4j, and MongoDB Atlas. The guide covers modeling an HR Policy system, setting up a Helidon MicroProfile project with Java 21, configuring dependencies (langchain4j-cdi 1.3.4, mongodb-driver-sync 5.9.0, langchain4j 1.16.0), creating CDI-managed AI agents, embedding text with OpenAI's text-embedding-3-small model, storing and searching vectors in MongoDB Atlas, and exposing REST endpoints via JAX-RS. Key patterns include duplicate detection using cosine similarity thresholds (minScore 0.95), semantic retrieval with configurable max-results and min-score, and CDI producers for clean dependency injection of the embedding model, vector store, and content retriever.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://foojay.io/today/introduction-to-retrieval-augmented-generation-with-java-and-mongodb>

## Questions this post answers

### How do I set up a RAG application in Java using LangChain4j and MongoDB Atlas?

A Java RAG application can be built with Helidon MicroProfile, LangChain4j CDI (version 1.3.4), and the langchain4j-mongodb-atlas extension (1.18.0-beta28). Text is embedded using OpenAI's text-embedding-3-small model, stored in a MongoDB Atlas vector collection, and retrieved via EmbeddingStoreContentRetriever with configurable minScore (default 0.7) and maxResults (default 3). The AI agent interface is declared with @RegisterAIService and injected via CDI.

_Java developers wiring up their first RAG pipeline can track LangChain4j and MongoDB Atlas releases on daily.dev._

### How can I prevent duplicate embeddings when ingesting documents into a MongoDB Atlas vector store with LangChain4j?

Before ingesting a new document, embed it and run a similarity search against the existing vector store using EmbeddingSearchRequest with a high minScore threshold (0.95 in this pattern). If the search returns any matches, skip ingestion and return an appropriate response. Only when no similar embedding is found should you proceed with EmbeddingStoreIngestor using a recursive document splitter (chunk size 100, overlap 10).

_Teams building knowledge-base ingestion pipelines can follow MongoDB Atlas and LangChain4j patterns on daily.dev._

### What Maven dependencies do I need for a LangChain4j RAG app with MongoDB Atlas and OpenAI embeddings?

You need mongodb-driver-sync 5.9.0, langchain4j-cdi-portable-ext and langchain4j-cdi-config at version 1.3.4, langchain4j-open-ai 1.16.0, and both langchain4j-embeddings-all-minilm-l6-v2 and langchain4j-mongodb-atlas at 1.18.0-beta28. The project targets Java 21 and uses Maven with MicroProfile configuration for credential management.

_Developers keeping dependency versions current for AI Java projects can watch for updates on daily.dev._

## Similar posts on daily.dev

- [Building a RAG Application with Spring Boot, Spring AI, MongoDB Atlas Vector Search, and OpenAI](https://daily.dev/posts/building-a-rag-application-with-spring-boot-spring-ai-mongodb-atlas-vector-search-and-openai-vhyy5w1do) · InfoQ · 1 upvotes · 0 comments
- [RAG with Spring AI](https://daily.dev/posts/rag-with-spring-ai-aalrsnemm) · Javarevisited · 6 upvotes · 0 comments
- [CodeLab: Building a RAG Application With Couchbase Capella Model Services and LangChain](https://daily.dev/posts/codelab-building-a-rag-application-with-couchbase-capella-model-services-and-langchain-u14vix8ez) · Couchbase · 1 upvotes · 0 comments

---

Tags: [#java](https://daily.dev/tags/java), [#mongodb](https://daily.dev/tags/mongodb), [#rag](https://daily.dev/tags/rag), [#vector-search](https://daily.dev/tags/vector-search), [#langchain4j](https://daily.dev/tags/langchain4j)

[View this post on daily.dev](https://daily.dev/posts/introduction-to-retrieval-augmented-generation-with-java-and-mongodb-jnxie1wns)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Introduction to Retrieval-Augmented Generation with Java and MongoDB","url":"https://daily.dev/posts/introduction-to-retrieval-augmented-generation-with-java-and-mongodb-jnxie1wns","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/introduction-to-retrieval-augmented-generation-with-java-and-mongodb-jnxie1wns"},"datePublished":"2026-08-10T10:20:53.337Z","dateModified":"2026-08-10T10:21:30.173Z","description":"A step-by-step tutorial for building a Retrieval-Augmented Generation (RAG) application in Java using Jakarta EE, LangChain4j, and MongoDB Atlas. The guide...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/429e0e46e41bafff5a6e3e676082b88e?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/429e0e46e41bafff5a6e3e676082b88e?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Foojay.io","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Foojay.io","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/1820f6a6ae944760aa322b67ed85f848","url":"https://daily.dev/sources/foojayio"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/introduction-to-retrieval-augmented-generation-with-java-and-mongodb-jnxie1wns","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java,mongodb,rag,vector-search,langchain4j","timeRequired":"PT11M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Foojay.io","item":"https://daily.dev/sources/foojayio"},{"@type":"ListItem","position":3,"name":"Introduction to Retrieval-Augmented Generation with Java and MongoDB"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/introduction-to-retrieval-augmented-generation-with-java-and-mongodb-jnxie1wns#faq","mainEntity":[{"@type":"Question","name":"How do I set up a RAG application in Java using LangChain4j and MongoDB Atlas?","acceptedAnswer":{"@type":"Answer","text":"A Java RAG application can be built with Helidon MicroProfile, LangChain4j CDI (version 1.3.4), and the langchain4j-mongodb-atlas extension (1.18.0-beta28). Text is embedded using OpenAI's text-embedding-3-small model, stored in a MongoDB Atlas vector collection, and retrieved via EmbeddingStoreContentRetriever with configurable minScore (default 0.7) and maxResults (default 3). The AI agent interface is declared with @RegisterAIService and injected via CDI. Java developers wiring up their first RAG pipeline can track LangChain4j and MongoDB Atlas releases on daily.dev."}},{"@type":"Question","name":"How can I prevent duplicate embeddings when ingesting documents into a MongoDB Atlas vector store with LangChain4j?","acceptedAnswer":{"@type":"Answer","text":"Before ingesting a new document, embed it and run a similarity search against the existing vector store using EmbeddingSearchRequest with a high minScore threshold (0.95 in this pattern). If the search returns any matches, skip ingestion and return an appropriate response. Only when no similar embedding is found should you proceed with EmbeddingStoreIngestor using a recursive document splitter (chunk size 100, overlap 10). Teams building knowledge-base ingestion pipelines can follow MongoDB Atlas and LangChain4j patterns on daily.dev."}},{"@type":"Question","name":"What Maven dependencies do I need for a LangChain4j RAG app with MongoDB Atlas and OpenAI embeddings?","acceptedAnswer":{"@type":"Answer","text":"You need mongodb-driver-sync 5.9.0, langchain4j-cdi-portable-ext and langchain4j-cdi-config at version 1.3.4, langchain4j-open-ai 1.16.0, and both langchain4j-embeddings-all-minilm-l6-v2 and langchain4j-mongodb-atlas at 1.18.0-beta28. The project targets Java 21 and uses Maven with MicroProfile configuration for credential management. Developers keeping dependency versions current for AI Java projects can watch for updates on daily.dev."}}]}
```

