# MongoDBAtlasVectorSearch

> **Class** in `langchain_mongodb`

📖 [View in docs](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch)

MongoDB Atlas vector store integration.

MongoDBAtlasVectorSearch performs data operations on
text, embeddings and arbitrary data. In addition to CRUD operations,
the VectorStore provides Vector Search
based on similarity of embedding vectors following the
Hierarchical Navigable Small Worlds (HNSW) algorithm.

This supports a number of models to ascertain scores,
"similarity" (default), "MMR", and "similarity_score_threshold".
These are described in the search_type argument to as_retriever,
which provides the Runnable.invoke(query) API, allowing
MongoDBAtlasVectorSearch to be used within a chain.

## Signature

```python
MongoDBAtlasVectorSearch(
    self,
    collection: Collection[Dict[str, Any]],
    embedding: Embeddings | str,
    index_name: str = 'vector_index',
    text_key: str | List[str] = 'text',
    embedding_key: str | None = 'embedding',
    relevance_score_fn: str | None = 'cosine',
    dimensions: int = -1,
    auto_create_index: bool | None = None,
    auto_index_timeout: int = 15,
    vector_index_options: dict | None = None,
    **kwargs: Any = {},
)
```

## Description

**Setup:**

* Set up a MongoDB Atlas cluster. The free tier M0 will allow you to start.
  Search Indexes are only available on Atlas, the fully managed cloud service,
  not the self-managed MongoDB.
  Follow [this guide](https://www.mongodb.com/basics/mongodb-atlas-tutorial)

* Create a Collection and a Vector Search Index.  The procedure is described
  [here](https://www.mongodb.com/docs/atlas/atlas-vector-search/create-index/#procedure).
  You can optionally supply a `dimensions` argument to programmatically create a Vector
  Search Index.

* Install ``langchain-mongodb``

.. code-block:: bash

    pip install -qU langchain-mongodb pymongo

.. code-block:: python

    import getpass
    MONGODB_ATLAS_CONNECTION_STRING = getpass.getpass("MongoDB Atlas Connection String:")

Key init args — indexing params:
    embedding: Embeddings
        Embedding function to use.

Key init args — client params:
    collection: Collection
        MongoDB collection to use.
    index_name: str
        Name of the Atlas Search index.

**Instantiate:**

.. code-block:: python

from pymongo import MongoClient
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch
from pymongo import MongoClient
from langchain_openai import OpenAIEmbeddings

vector_store = MongoDBAtlasVectorSearch.from_connection_string(
    connection_string=os=MONGODB_ATLAS_CONNECTION_STRING,
    namespace="db_name.collection_name",
    embedding=OpenAIEmbeddings(),
    index_name="vector_index",
    text_key="text_field"
)

**Add Documents:**

.. code-block:: python

from langchain_core.documents import Document

document_1 = Document(page_content="foo", metadata={"baz": "bar"})
document_2 = Document(page_content="thud", metadata={"bar": "baz"})
document_3 = Document(page_content="i will be deleted :(")

documents = [document_1, document_2, document_3]
ids = ["1", "2", "3"]
vector_store.add_documents(documents=documents, ids=ids)

**Delete Documents:**

.. code-block:: python

vector_store.delete(ids=["3"])

**Search:**

.. code-block:: python

    results = vector_store.similarity_search(query="thud",k=1)
    for doc in results:
        print(f"* {doc.page_content} [{doc.metadata}]")

.. code-block:: python

    * thud [{'_id': '2', 'baz': 'baz'}]

**Search with filter:**

.. code-block:: python

    results = vector_store.similarity_search(query="thud",k=1,post_filter=[{"bar": "baz"]})
    for doc in results:
        print(f"* {doc.page_content} [{doc.metadata}]")

.. code-block:: python

    * thud [{'_id': '2', 'baz': 'baz'}]

**Search with score:**

.. code-block:: python

    results = vector_store.similarity_search_with_score(query="qux",k=1)
    for doc, score in results:
        print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")

.. code-block:: python

    * [SIM=0.916096] foo [{'_id': '1', 'baz': 'bar'}]

**Async:**

.. code-block:: python

    # add documents
    # await vector_store.aadd_documents(documents=documents, ids=ids)

    # delete documents
    # await vector_store.adelete(ids=["3"])

    # search
    # results = vector_store.asimilarity_search(query="thud",k=1)

    # search with score
    results = await vector_store.asimilarity_search_with_score(query="qux",k=1)
    for doc,score in results:
        print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")

.. code-block:: python

    * [SIM=0.916096] foo [{'_id': '1', 'baz': 'bar'}]

Use as Retriever:

.. code-block:: python

    retriever = vector_store.as_retriever(
        search_type="mmr",
        search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
    )
    retriever.invoke("thud")

.. code-block:: python

    [Document(metadata={'_id': '2', 'embedding': [-0.01850726455450058, -0.0014740974875167012, -0.009762819856405258, ...], 'baz': 'baz'}, page_content='thud')]

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `collection` | `Collection[Dict[str, Any]]` | Yes | MongoDB collection to add the texts to |
| `embedding` | `Embeddings \| str` | Yes | Text embedding model to use.  If a string is passed, it will be used to create an AutoEmbeddings class with the given model name. |
| `text_key` | `str \| List[str]` | No | MongoDB field that will contain the text for each document. It is possible to parse a list of fields.            The first one will be used as text key. Default: 'text' (default: `'text'`) |
| `index_name` | `str` | No | Existing Atlas Vector Search Index (default: `'vector_index'`) |
| `embedding_key` | `str \| None` | No | Field that will contain the embedding for each document, should be `None` if embedding is an instance of `AutoEmbeddings`. (default: `'embedding'`) |
| `relevance_score_fn` | `str \| None` | No | The similarity score used for the index Currently supported: 'euclidean', 'cosine', and 'dotProduct' Should be `None` if embedding is an AutoEmbedding. (default: `'cosine'`) |
| `auto_create_index` | `bool \| None` | No | Whether to automatically create an index if it does not exist.  By default, if no search index of `index_name` exists, one will be created. (default: `None`) |
| `dimensions` | `int` | No | Number of dimensions in embedding.  If the value is not provided, and `auto_create_index` is `true`, the value will be inferred. Should be `-1` if embedding is an instance of `AutoEmbeddings`. (default: `-1`) |
| `auto_index_timeout` | `int` | No | Timeout in seconds to wait for an auto-created index to be ready. (default: `15`) |

## Extends

- `VectorStore`

## Constructors

```python
__init__(
    self,
    collection: Collection[Dict[str, Any]],
    embedding: Embeddings | str,
    index_name: str = 'vector_index',
    text_key: str | List[str] = 'text',
    embedding_key: str | None = 'embedding',
    relevance_score_fn: str | None = 'cosine',
    dimensions: int = -1,
    auto_create_index: bool | None = None,
    auto_index_timeout: int = 15,
    vector_index_options: dict | None = None,
    **kwargs: Any = {},
) -> None
```

| Name | Type |
|------|------|
| `collection` | `Collection[Dict[str, Any]]` |
| `embedding` | `Embeddings \| str` |
| `index_name` | `str` |
| `text_key` | `str \| List[str]` |
| `embedding_key` | `str \| None` |
| `relevance_score_fn` | `str \| None` |
| `dimensions` | `int` |
| `auto_create_index` | `bool \| None` |
| `auto_index_timeout` | `int` |
| `vector_index_options` | `dict \| None` |


## Properties

- `embeddings`
- `collection`

## Methods

- [`from_connection_string()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/from_connection_string)
- [`close()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/close)
- [`add_texts()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/add_texts)
- [`get_by_ids()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/get_by_ids)
- [`bulk_embed_and_insert_texts()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/bulk_embed_and_insert_texts)
- [`add_documents()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/add_documents)
- [`similarity_search_with_score()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/similarity_search_with_score)
- [`similarity_search()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/similarity_search)
- [`max_marginal_relevance_search()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/max_marginal_relevance_search)
- [`from_texts()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/from_texts)
- [`delete()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/delete)
- [`adelete()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/adelete)
- [`max_marginal_relevance_search_by_vector()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/max_marginal_relevance_search_by_vector)
- [`amax_marginal_relevance_search_by_vector()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/amax_marginal_relevance_search_by_vector)
- [`create_vector_search_index()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/create_vector_search_index)
- [`similarity_search_by_vector()`](https://reference.langchain.com/python/langchain-mongodb/vectorstores/MongoDBAtlasVectorSearch/similarity_search_by_vector)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-mongodb/blob/ad9050c28e092b335dcb846f77c0ec2245553f79/libs/langchain-mongodb/langchain_mongodb/vectorstores.py#L57)