Skip to main content

MongoDBVectorSearchTool

Description

The MongoDBVectorSearchTool enables vector similarity queries on MongoDB Atlas collections. MongoDB Atlas supports native vector search. The tool also supports index creation helpers and bulk insert of embedded texts. MongoDB Vector Search is useful for applications that want to use a vector search database to query data with semantic search, filter queries on other fields in your collection, or power your AI applications and agentic systems by implementing retrieval-augmented generation (RAG). Learn more: https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-overview/

Installation

Install with the MongoDB extra:
or

Initialization Parameters

  • connection_string (str): Required. MongoDB Atlas connection string credentials to connect to your cluster.
  • database_name (str): Required. The name of the MongoDB database to use.
  • collection_name (str): Required. The collection within the database where documents and embeddings are stored.
  • vector_index_name (str, default: vector_index): Optional. The name of the MongoDB Vector Search index to query (must match the index created in Atlas).
  • text_key (str, default: text): Optional. The document field name where raw text is stored.
  • embedding_key (str, default: embedding): Optional. The document field name where the vector embedding is stored.
  • dimensions (int, default: 1536): Optional. The number of dimensions for the embedding model (must match your model).
  • query_config (MongoDBVectorSearchConfig): Optional. A MongoDBVectorSearchConfig object for advanced query settings, like limit and oversampling_factor.

MongoDBVectorSearchTool Methods

  • run(query=...): Embeds a query and performs vector similarity search, returning matching documents. Takes a natural language query (str) parameter to embed and search.
  • add_texts(list of strings): Takes a list of strings to embed and insert as documents into your MongoDB Atlas database.
  • create_vector_search_index(dimensions=...): Provisions a MongoDB Vector Search index with the correct dimensions and similarity. Takes a dimensions (int) parameter.

Quick start

The following example demonstrates how to initialize the tool and execute a vector similarity search against the specified MongoDB Atlas collection:
Code

Common issues

  • Authentication failures: ensure your Atlas IP Access List allows your runner and the connection string includes credentials.
  • Index not found: create the vector index first; name must match vector_index_name.
  • Dimensions mismatch: align embedding model dimensions with dimensions.

Examples

Preload the database and create the index

The following example demonstrates how to preload your MongoDB Atlas database with documents and create a MongoDB Vector Search index to query your collection:
Code

Custom query configuration and agent configuration

The following example demonstrates how to provide a custom query configuration to the MongoDBVectorSearchTool and integrate with a CrewAI agent:
Code

Run a CrewAI workflow

The following example demonstrates how to integrate your MongoDBVectorSearchTool with an Agent, Task, and Crew to create and run a CrewAI agentic RAG workflow:
Code

Learn More

To learn more about CrewAI integrations with MongoDB, see the following MongoDB guides: