MongoDB-RAG (Retrieval Augmented Generation) is an NPM module that simplifies vector search using MongoDB Atlas. This library enables developers to efficiently perform similarity search, caching, batch processing, and indexing for fast and accurate retrieval of relevant data.
- Vector Search: Efficiently retrieves similar documents using MongoDB's Atlas Vector Search.
- Dynamic Database & Collection Selection: Supports flexible selection of multiple databases and collections.
- Batch Processing: Handles bulk processing of documents with retry mechanisms.
- Index Management: Ensures necessary indexes are available and optimized.
- Caching Mechanism: Provides in-memory caching for frequently accessed data.
- Advanced Chunking: Supports sliding window, semantic, and recursive chunking strategies.
- CLI for Scaffolding RAG Apps
npm install mongodb-rag dotenv
- Create a MongoDB Atlas Cluster (MongoDB Atlas)
- Enable Vector Search under Indexes:
{ "definition": { "fields": [ { "path": "embedding", "type": "vector", "numDimensions": 1536, "similarity": "cosine" } ] } }
- Get Your Connection String and store it in
.env
:MONGODB_URI=mongodb+srv://<username>:<password>@cluster0.mongodb.net/ EMBEDDING_PROVIDER=openai # Options: openai, deepseek EMBEDDING_API_KEY=your-embedding-api-key EMBEDDING_MODEL=text-embedding-3-small # Change based on provider VECTOR_INDEX=default
You can generate a fully working RAG-enabled app with MongoDB Atlas Vector Search using:
npx mongodb-rag create-rag-app my-rag-app
This will:
- Scaffold a new CRUD RAG app with Express and MongoDB Atlas.
- Set up environment variables for embedding providers.
- Create API routes for ingestion, search, and deletion.
Then, navigate into your project and run:
cd my-rag-app
npm install
npm run dev
import { MongoRAG } from 'mongodb-rag';
import dotenv from 'dotenv';
dotenv.config();
const rag = new MongoRAG({
mongoUrl: process.env.MONGODB_URI,
database: 'my_rag_db', // Default database
collection: 'documents', // Default collection
embedding: {
provider: process.env.EMBEDDING_PROVIDER,
apiKey: process.env.EMBEDDING_API_KEY,
model: process.env.EMBEDDING_MODEL,
dimensions: 1536
}
});
await rag.connect();
const documents = [
{ id: 'doc1', content: 'MongoDB is a NoSQL database.', metadata: { source: 'docs' } },
{ id: 'doc2', content: 'Vector search is useful for semantic search.', metadata: { source: 'ai' } }
];
await rag.ingestBatch(documents, { database: 'dynamic_db', collection: 'dynamic_docs' });
console.log('Documents ingested.');
const query = 'How does vector search work?';
const results = await rag.search(query, {
database: 'dynamic_db',
collection: 'dynamic_docs',
maxResults: 3
});
console.log('Search Results:', results);
await rag.close();
Store embeddings in multiple databases and collections dynamically.
await rag.ingestBatch(docs, { database: 'finance_db', collection: 'reports' });
const results = await rag.search('AI topics', {
database: 'my_rag_db',
collection: 'documents',
maxResults: 5,
filter: { 'metadata.source': 'ai' }
});
Run tests using:
npm test
Run in watch mode:
npm run test:watch
Check test coverage:
npm run test:coverage
Contributions are welcome! Please fork the repository and submit a pull request.
This project is licensed under the MIT License.
- For more examples, check our examples directory.
- CLI Reference
- Documentation
- GitHub Repository
- Bug Reports
- MongoDB Atlas