> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ag2.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# autogen.agentchat.contrib.vectordb.mongodb.MongoDBAtlasVectorDB

<h2 id="autogen.agentchat.contrib.vectordb.mongodb.MongoDBAtlasVectorDB" class="doc doc-heading">
  <code class="doc-symbol doc-symbol-heading doc-symbol-class" />

  <span class="doc doc-object-name doc-class-name">MongoDBAtlasVectorDB</span>
</h2>

```python theme={null}
MongoDBAtlasVectorDB(
    connection_string: str = '',
    database_name: str = 'vector_db',
    embedding_function: Callable[..., Any] | None = None,
    collection_name: str = None,
    index_name: str = 'vector_index',
    overwrite: bool = False,
    wait_until_index_ready: float | None = None,
    wait_until_document_ready: float | None = None
)
```

A Collection object for MongoDB.<br />Initialize the vector database.<br />

<b>Parameters:</b>

| Name                        | Description                                                        |
| --------------------------- | ------------------------------------------------------------------ |
| `connection_string`         | **Type:** str<br /><br />**Default:** ''                           |
| `database_name`             | **Type:** str<br /><br />**Default:** 'vector\_db'                 |
| `embedding_function`        | **Type:** Callable\[..., Any] \| None<br /><br />**Default:** None |
| `collection_name`           | **Type:** str<br /><br />**Default:** None                         |
| `index_name`                | **Type:** str<br /><br />**Default:** 'vector\_index'              |
| `overwrite`                 | **Type:** bool<br /><br />**Default:** False                       |
| `wait_until_index_ready`    | **Type:** float \| None<br /><br />**Default:** None               |
| `wait_until_document_ready` | **Type:** float \| None<br /><br />**Default:** None               |

### Class Attributes

<code class="doc-symbol doc-symbol-heading doc-symbol-attribute" />

#### active\_collection

<br />

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-attribute" />

#### embedding\_function

<br />

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-attribute" />

#### type

<br />

<br />

### Instance Methods

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### create\_collection

```python theme={null}
create_collection(
    self,
    collection_name: str,
    overwrite: bool = False,
    get_or_create: bool = True
) -> Collection
```

Create a collection in the vector database and create a vector search index in the collection.<br />

<b>Parameters:</b>

| Name              | Description |                                                                                                                                        |
| ----------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `collection_name` | str         | The name of the collection.<br /><br />**Type:** str                                                                                   |
| `overwrite`       | bool        | Whether to overwrite the collection if it exists.<br /><br />Default is False.<br /><br />**Type:** bool<br /><br />**Default:** False |
| `get_or_create`   | bool        | Whether to get or create the collection.<br /><br />Default is True<br /><br />**Type:** bool<br /><br />**Default:** True             |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### create\_index\_if\_not\_exists

```python theme={null}
create_index_if_not_exists(
    self,
    index_name: str = 'vector_index',
    collection: Collection = None
) -> 
```

Creates a vector search index on the specified collection in MongoDB.<br />

<b>Parameters:</b>

| Name         | Description                                                                                                                                                     |
| ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `index_name` | The name of the vector search index to create.<br /><br />Defaults to "vector\_search\_index".<br /><br />**Type:** str<br /><br />**Default:** 'vector\_index' |
| `collection` | The MongoDB collection to create the index on.<br /><br />Defaults to None.<br /><br />**Type:** Collection<br /><br />**Default:** None                        |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### create\_vector\_search\_index

```python theme={null}
create_vector_search_index(
    self,
    collection: Collection,
    index_name: str | None = 'vector_index',
    similarity: Literal['euclidean', 'cosine', 'dotProduct'] = 'cosine'
) -> 
```

Create a vector search index in the collection.<br />

<b>Parameters:</b>

| Name         | Description                                                                                                                                         |
| ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `collection` | An existing Collection in the Atlas Database.<br /><br />**Type:** Collection                                                                       |
| `index_name` | Vector Search Index name.<br /><br />**Type:** str \| None<br /><br />**Default:** 'vector\_index'                                                  |
| `similarity` | Algorithm used for measuring vector similarity.<br /><br />**Type:** Literal\['euclidean', 'cosine', 'dotProduct']<br /><br />**Default:** 'cosine' |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### delete\_collection

```python theme={null}
delete_collection(self, collection_name: str) -> None
```

Delete the collection from the vector database.<br />

<b>Parameters:</b>

| Name              | Description |                                                      |
| ----------------- | ----------- | ---------------------------------------------------- |
| `collection_name` | str         | The name of the collection.<br /><br />**Type:** str |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### delete\_docs

```python theme={null}
delete_docs(
    self,
    ids: list[str | int],
    collection_name: str = None,
    **kwargs
) -> 
```

Delete documents from the collection of the vector database.<br />

<b>Parameters:</b>

| Name              | Description                                                                                                   |
| ----------------- | ------------------------------------------------------------------------------------------------------------- |
| `ids`             | A list of document ids.<br /><br />Each id is a typed `ItemID`.<br /><br />**Type:** list\[str \| int]        |
| `collection_name` | The name of the collection.<br /><br />Default is None.<br /><br />**Type:** str<br /><br />**Default:** None |
| `**kwargs`        | Additional keyword arguments.<br /><br />                                                                     |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### get\_collection

```python theme={null}
get_collection(self, collection_name: str = None) -> Collection
```

Get the collection from the vector database.<br />

<b>Parameters:</b>

| Name              | Description |                                                                                                                                                                         |
| ----------------- | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `collection_name` | str         | The name of the collection.<br /><br />Default is None.<br /><br />If None, return the current active collection.<br /><br />**Type:** str<br /><br />**Default:** None |

<b>Returns:</b>

| Type       | Description                          |
| ---------- | ------------------------------------ |
| Collection | Collection \| The collection object. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### get\_docs\_by\_ids

```python theme={null}
get_docs_by_ids(
    self,
    ids: list[str | int] = None,
    collection_name: str = None,
    include: list[str] = None,
    **kwargs
) -> list[Document]
```

Retrieve documents from the collection of the vector database based on the ids.<br />

<b>Parameters:</b>

| Name              | Description   |                                                                                                                                                                                                                                                                   |
| ----------------- | ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ids`             | List\[ItemID] | A list of document ids.<br /><br />If None, will return all the documents.<br /><br />Default is None.<br /><br />**Type:** list\[str \| int]<br /><br />**Default:** None                                                                                        |
| `collection_name` | str           | The name of the collection.<br /><br />Default is None.<br /><br />**Type:** str<br /><br />**Default:** None                                                                                                                                                     |
| `include`         | List\[str]    | The fields to include.<br /><br />If None, will include \["metadata", "content"], ids will always be included.<br /><br />Basically, use include to choose whether to include embedding and metadata<br /><br />**Type:** list\[str]<br /><br />**Default:** None |
| `**kwargs`        |               |                                                                                                                                                                                                                                                                   |

<b>Returns:</b>

| Type                                                                                    | Description                     |
| --------------------------------------------------------------------------------------- | ------------------------------- |
| list\[[Document](/docs/api-reference/autogen/agentchat/contrib/vectordb/base/Document)] | List\[Document] \| The results. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### insert\_docs

```python theme={null}
insert_docs(
    self,
    docs: list[Document],
    collection_name: str = None,
    upsert: bool = False,
    batch_size: int = 100000,
    **kwargs: Any
) -> None
```

Insert Documents and Vector Embeddings into the collection of the vector database.<br />For large numbers of Documents, insertion is performed in batches.<br />

<b>Parameters:</b>

| Name              | Description                                                                                                                                                                           |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `docs`            | A list of documents.<br /><br />Each document is a TypedDict `Document`.<br /><br />**Type:** list\[[Document](/docs/api-reference/autogen/agentchat/contrib/vectordb/base/Document)] |
| `collection_name` | The name of the collection.<br /><br />Default is None.<br /><br />**Type:** str<br /><br />**Default:** None                                                                         |
| `upsert`          | Whether to update the document if it exists.<br /><br />Default is False.<br /><br />**Type:** bool<br /><br />**Default:** False                                                     |
| `batch_size`      | Number of documents to be inserted in each batch<br /><br />**Type:** int<br /><br />**Default:** 100000                                                                              |
| `**kwargs`        | Additional keyword arguments.<br /><br />**Type:** Any                                                                                                                                |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### list\_collections

```python theme={null}
list_collections(self) -> 
```

List the collections in the vector database.<br />Returns: <br />    List\[str] | The list of collections.

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### retrieve\_docs

```python theme={null}
retrieve_docs(
    self,
    queries: list[str],
    collection_name: str = None,
    n_results: int = 10,
    distance_threshold: float = -1,
    **kwargs: Any
) -> list[list[tuple[Document, float]]]
```

Retrieve documents from the collection of the vector database based on the queries.<br />

<b>Parameters:</b>

| Name                 | Description   |                                                                                                                                                                                                   |
| -------------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `queries`            | List\[str]    | A list of queries.<br /><br />Each query is a string.<br /><br />**Type:** list\[str]                                                                                                             |
| `collection_name`    | str           | The name of the collection.<br /><br />Default is None.<br /><br />**Type:** str<br /><br />**Default:** None                                                                                     |
| `n_results`          | int           | The number of relevant documents to return.<br /><br />Default is 10.<br /><br />**Type:** int<br /><br />**Default:** 10                                                                         |
| `distance_threshold` | float         | The threshold for the distance score, only distance smaller than it will be returned.<br /><br />Don't filter with it if  0. Default is -1.<br /><br />**Type:** float<br /><br />**Default:** -1 |
| `**kwargs`           | **Type:** Any |                                                                                                                                                                                                   |

<b>Returns:</b>

| Type                                                                                                          | Description                                                                          |
| ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ |
| list\[list\[tuple\[[Document](/docs/api-reference/autogen/agentchat/contrib/vectordb/base/Document), float]]] | QueryResults \| For each query string, a list of nearest documents and their scores. |

<br />

<code class="doc-symbol doc-symbol-heading doc-symbol-method" />

#### update\_docs

```python theme={null}
update_docs(
    self,
    docs: list[Document],
    collection_name: str = None,
    **kwargs: Any
) -> None
```

Update documents, including their embeddings, in the Collection.<br />Optionally allow upsert as kwarg.<br />Uses deepcopy to avoid changing docs.<br />

<b>Parameters:</b>

| Name              | Description     |                                                                                                                                   |
| ----------------- | --------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `docs`            | List\[Document] | A list of documents.<br /><br />**Type:** list\[[Document](/docs/api-reference/autogen/agentchat/contrib/vectordb/base/Document)] |
| `collection_name` | str             | The name of the collection.<br /><br />Default is None.<br /><br />**Type:** str<br /><br />**Default:** None                     |
| `**kwargs`        | **Type:** Any   |                                                                                                                                   |

<br />
