How to Set Up MongoDB Atlas and Vector Search
Author: Shaurya Sharma
This guide provides a step-by-step process for setting up MongoDB Atlas and enabling vector search capabilities via the dashboard. MongoDB Atlas is a cloud database service that simplifies deployment and management of MongoDB clusters.
Step 1: Create a MongoDB Atlas Account
- Visit MongoDB Atlas.
- Click Sign Up if you don’t already have an account or log in with your existing credentials.
Step 2: Create a New Cluster
- After logging in, click Create a Cluster on the dashboard.
- Select a M0 for free-tier usage or choose a paid option for more resources.
- Configure your cluster:
- Select your cloud provider (AWS, Azure, or GCP).
- Choose a region that is geographically closest to your users for better performance.
- Click Create Deployment and wait for the cluster to be provisioned (this can take a few minutes).
Step 3: Set Up a Database User
After deployment, it will ask you to create a database user. Simply choose the username ans pasword and click on Create Database User.
OR
- Go to Database Access in the left-hand menu.
- Click Add New Database User.
- Set up the authentication method:
- Use Username/Password.
- Enter a username and secure password (e.g., generated by a password manager).
- Assign user privileges:
- Select Read and Write to Any Database for general access.
- Save the user settings.
Step 4: Configure Network Access
- Navigate to Network Access in the left-hand menu.
- Click Add IP Address.
- Select Allow Access from Anywhere (or specify your IP address for enhanced security).
- Click Confirm to save the changes.
Step 5: Connect to Your Cluster
- In the Clusters section, click Connect next to your cluster.
- Choose your connection method:
- Connect Your Application: Provides a connection string for use in your application.
- MongoDB Compass: A GUI client for interacting with your database.
- Copy the connection string provided.
Step 6: Enable Vector Search
-
Navigate to Collections in your cluster dashboard.
-
Click Create Database and name it (e.g.,
chatbot_data
). -
Add a collection within the database (e.g.,
documents
). -
Go to the Atlas Search tab within the collection.
-
Click Create Search Index and select the Atlas Vector Search type and JSON Editor.
-
Select the database and collection to index. (e.g.
db_main
->chatbot_data
). -
Configure the index:
- For
chatbot_data
collection:{
"fields": [
{
"numDimensions": 1536,
"path": "data_embedding",
"similarity": "cosine",
"type": "vector"
}
]
} - For
report_chatting
collection:{
"fields": [
{
"numDimensions": 1536,
"path": "data_embedding",
"similarity": "cosine",
"type": "vector"
},
{
"type": "filter",
"path": "country_name"
},
{
"type": "filter",
"path": "year"
}
]
}
- For
-
Save the index.
Step 7: Test the Connection
-
Use a MongoDB client (e.g., Compass or your application) to connect to the cluster using the connection string.
-
Query the collection to ensure data is accessible:
db.documents.find({})
-
For vector search, use the
$vectorSearch
query operator (available with supported MongoDB Atlas versions):db.documents.aggregate([
{
"$search": {
"index": "data_embedding",
"text": {
"query": "your_query",
"path": "data_embedding"
}
}
}
])
Step 8: Monitor and Optimize
- Use the Metrics section in MongoDB Atlas to monitor database performance.
- Review Index Usage to ensure the vector search index is effectively utilized.
- Scale your cluster if needed based on usage and performance metrics.
Important Notes
After performing a database update or reset, ensure that the search indexes are not deleted. If they are, recreate them.