Setting up MongoDB Atlas for Vector Search
In this section, you'll create and configure a MongoDB Atlas cluster with Vector Search capabilities to serve as the foundation for your RAG application.
Creating a MongoDB Atlas Account
If you don't already have a MongoDB Atlas account, you'll need to create one:
- Go to MongoDB Atlas
- Sign up for a free account
- Complete the initial setup process
Deploying a MongoDB Atlas Cluster
Once you have an account, follow these steps to create a new cluster:
- Log in to your MongoDB Atlas account
- Click the "Build a Database" button
- Choose your preferred cloud provider (AWS, GCP, or Azure)
- Select the "M0 Free Tier" cluster for this workshop
- Choose a region close to your location
- Click "Create" to deploy your cluster
insert screenshot here
While your cluster is being created (which takes a few minutes), let's configure access settings.
Configuring Database Access
You'll need to create a database user to connect to your cluster:
- In the sidebar, click "Database Access" under SECURITY
- Click "Add New Database User"
- Enter a username and password (save these credentials!)
- Select "Built-in Role" and choose "Read and write to any database"
- Click "Add User"
Store your database credentials securely. You'll need them to connect to your cluster.
Configuring Network Access
Next, configure network access to allow connections from your development environment:
- In the sidebar, click "Network Access" under SECURITY
- Click "Add IP Address"
- For this workshop, select "Allow Access from Anywhere" (not recommended for production)
- Click "Confirm"
Loading Sample Data (Optional)
MongoDB Atlas provides sample datasets that you can use for this workshop:
- On your cluster's overview page, click "Browse Collections"
- Click "Load Sample Dataset"
- Wait for the sample data to be loaded
insert screenshot here
Getting Your Connection String
To connect to your cluster from your application, you'll need your connection string:
- On your cluster's overview page, click "Connect"
- Select "Connect your application"
- Choose "Node.js" as your driver and the appropriate version
- Copy the connection string
- Replace
<password>
with your database user's password
Save this connection string - you'll use it in the next sections.
Creating a .env File
Let's create a .env
file to store your configuration. Create a new file named .env
in your project directory with the following content:
MONGODB_URI=mongodb+srv://username:password@clustername.mongodb.net/
EMBEDDING_PROVIDER=openai # Can be openai, ollama, etc.
EMBEDDING_API_KEY=your-api-key-here # Your OpenAI API key or leave empty for Ollama
EMBEDDING_MODEL=text-embedding-3-small # For OpenAI or your preferred model
Replace the placeholder values with your actual credentials.
For the CLI Gurus
If you're a CLI guru, you can use the mdb
CLI to create a new cluster and connect to it.
npx mongodb-rag init
This will prompt you for all of the variables required to build a RAG App.
Once you have the .mongodb-rag.json file created, you can also create a .env from the same values using the following:
npx mongodb-rag create-env
Testing Your Connection
Let's verify that your connection is working. Create a file named test-connection.js
:
const { MongoClient } = require('mongodb');
require('dotenv').config();
async function testConnection() {
const uri = process.env.MONGODB_URI;
const client = new MongoClient(uri);
try {
await client.connect();
console.log('✅ Successfully connected to MongoDB Atlas');
const databases = await client.db().admin().listDatabases();
console.log('Available databases:');
databases.databases.forEach(db => console.log(` - ${db.name}`));
} catch (error) {
console.error('❌ Connection failed:', error);
} finally {
await client.close();
}
}
testConnection();
Run the script with:
node test-connection.js
If you see "Successfully connected to MongoDB Atlas" and a list of databases, your connection is working correctly!
Next Steps
Now that you have set up MongoDB Atlas with Vector Search capabilities, you're ready to create vector embeddings and store them in your cluster.