Skip to main content

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

  1. Visit MongoDB Atlas.
  2. Click Sign Up if you don’t already have an account or log in with your existing credentials.

Step 2: Create a New Cluster

  1. After logging in, click Create a Cluster on the dashboard.
  2. Select a M0 for free-tier usage or choose a paid option for more resources.
  3. Configure your cluster:
    • Select your cloud provider (AWS, Azure, or GCP).
    • Choose a region that is geographically closest to your users for better performance.
  4. 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

  1. Go to Database Access in the left-hand menu.
  2. Click Add New Database User.
  3. Set up the authentication method:
    • Use Username/Password.
    • Enter a username and secure password (e.g., generated by a password manager).
  4. Assign user privileges:
    • Select Read and Write to Any Database for general access.
  5. Save the user settings.

Step 4: Configure Network Access

  1. Navigate to Network Access in the left-hand menu.
  2. Click Add IP Address.
  3. Select Allow Access from Anywhere (or specify your IP address for enhanced security).
  4. Click Confirm to save the changes.

Step 5: Connect to Your Cluster

  1. In the Clusters section, click Connect next to your cluster.
  2. 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.
  3. Copy the connection string provided.
  1. Navigate to Collections in your cluster dashboard.

  2. Click Create Database and name it (e.g., chatbot_data).

  3. Add a collection within the database (e.g., documents).

  4. Go to the Atlas Search tab within the collection.

  5. Click Create Search Index and select the Atlas Vector Search type and JSON Editor.

  6. Select the database and collection to index. (e.g. db_main -> chatbot_data).

  7. 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"
      }
      ]
      }
  8. Save the index.

Step 7: Test the Connection

  1. Use a MongoDB client (e.g., Compass or your application) to connect to the cluster using the connection string.

  2. Query the collection to ensure data is accessible:

     db.documents.find({})
  3. 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

  1. Use the Metrics section in MongoDB Atlas to monitor database performance.
  2. Review Index Usage to ensure the vector search index is effectively utilized.
  3. 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.