# MongoDBAtlasDocumentStore

Use the MongoDB database as the document store for storing data your pipelines can query.

***

## Basic Information

- Used with `MongoDBAtlasEmbeddingRetriever`
- Type: `haystack_integrations.document_stores.mongodb_atlas.document_store.MongoDBAtlasDocumentStore`

## Overview

For details, see [MongoDB documentation](https://www.mongodb.com/docs/atlas/) and [Haystack documentation](https://docs.haystack.deepset.ai/docs/mongodbatlasdocumentstore).

## Authorization

You must have an Atlas account. For details on setting it up, see [MongoDB documentation](https://www.mongodb.com/docs/atlas/getting-started/?tck=atlas_learn). 

To connect to the MongoDB database, you must provide a connection string in the format `"mongodb+srv://{mongo_atlas_username}:{mongo_atlas_password}@{mongo_atlas_host}/?{mongo_atlas_params_string}"`. For detailed instructions on how to obtain it, see [Create a Connection String](https://www.mongodb.com/docs/mongoid/current/quick-start-sinatra/create-a-connection-string/) in MongoDB documentation. 

Once you have your connection string, connect MongoDB to <ProductName /> on the Integrations page:

1. Log in to <ProductName />. 
2. Click your profile icon in the top right corner and choose *Settings*.
    <ClickableImage
      src="/img/getting-started/settings_option.png"
      alt="The menu expanded with the connections options highlighted"
      size="large"
    />

3. Go to *Workspace>Integrations*, find MongoDB, and click **Connect** next to it.
4. Paste your MongoDB connection string and click **Connect**.

## Usage

To configure MongoDB as the document store, you need:

- The name of the database to use.
- The name of the collection to use. This collection must have a vector search index set up on the `embedding` field and a full text search index.
- The name of the vector search index to use for vector search. You can create a vector search index on your collection in the Atlas web user interface.  
  **Important**: Your MongoDB vector search index must have an `embedding` field of type `knnVector` defined, for example:
  
  ```python
  const index = {  
           name: "vector_index",  
           type: "vectorSearch",  
           definition: {  
             "fields": [  
               {  
                 "type": "knnVector",  
                 "path": "embedding",  
                 "similarity": "cosine",  
                 "numDimensions": 768  
               }  
             ]  
           }  
       }
  ```
  This allows you to use embedding retrieval with the MongoDB document store.  
- The name of the full text search index. You create a full text search index on your collection in the Altas user interface.

You must create both a vector search index and a full text search index on your Atlas collection, one for embeddings and one for text. Vector search index is used for embedding-based similarity queries, while the full-text index is used for keyword queries. This way, you can use hybrid search out of the box.

For details, see [MongoDB documentation](https://www.mongodb.com/docs/compass/current/indexes/create-vector-search-index/).

### Writing Data to MongoDB

To write the preprocessed files into the MongoDB document store:

1. Add `DocumentWriter` to your index.
2. Add `MongoDBAtlasDocumentStore` and configure the required parameters on the component card. You must provide:
   - `database_name`: The name of your MongoDB database.
   - `collection_name`: The name of your collection.
   - `vector_search_index`: The name of the vector search index created on your collection. Make sure the index has an `embedding` field of type `knnVector` defined.
3. Connect `MongoDBAtlasDocumentStore` to `DocumentWriter`.

  
### Retrieving Files From MongoDB

To retrieve files from the MongoDB document store and use them for search:

1. Add a MongoDB Atlas Retriever your query pipeline.
2. Add `MongoDBAtlasDocumentStore`  and configure the required parameters on the component card. You must provide:
   - `database_name`: The name of your MongoDB database.
   - `collection_name`: The name of your collection.
   - `vector_search_index`: The name of the vector search index created on your collection. Make sure the index has an `embedding` field of type `knnVector` defined.
3. Connect `MongoDBAtlasDocumentStore` to the Retriever.

### Examples

This is how you connect the document store to writer:

<ClickableImage
  src="/img/concepts/documentwriter_configure_docstore.png"
  alt="The configure button on the DocumentWriter card"
  size="large"
/>

When you switch to YAML, you can see that the document store is a parameter of `DocumentWriter` and that's where you can configure it as well:

```yaml
writer:
    type: haystack.components.writers.document_writer.DocumentWriter
    init_parameters:
      document_store:
        type: haystack_integrations.document_stores.mongodb_atlas.document_store.MongoDBAtlasDocumentStore #document store configuration
        init_parameters:
          mongo_connection_string: 
            type: env_var
            env_vars:
            - MONGO_CONNECTION_STRING
            strict: false
          database_name: myDatabase
          collection_name: myCollection
          vector_search_index: vectorIndex
          full_text_search_index: fullTextIndex
      policy: OVERWRITE
```

## Init Parameters

| Parameter                | Type    | Possible values | Description                                                                                                                                                                                                                                                                             |
|--------------------------|---------|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `mongo_connection_string` | Secret  |                  | The connection string to connect <ProductShortName /> with MongoDB Atlas. You can obtain it by clicking the **CONNECT** button on the MongoDB Atlas Dashboard. For details, see the _Authorization_ section above.  <br/>Required.                                                               |
| `database_name`          | String  |                  | The name of your MongoDB Atlas database.  <br/>Required.                                                                                                                                                                                                                             |
| `collection_name`        | String  |                  | The name of the MongoDB Atlas collection you want to use. To use a collection for vector search, it must have a vector search index configured on the `embedding` field. For details, see the _Usage_ section above.  <br/>Required.                                               |
| `vector_search_index`    | String  |                  | The name of the MongoDB Atlas vector search index to use for embedding retrieval. Create a vector search index in the Atlas dashboard. For details, see the _Usage_ section above.  <br/>Required.                                                                                   |
| `full_text_search_index` | String  |                  | The name of the text search index to use for keyword retrieval. Create the index in your MongoDB Atlas dashboard.  <br/>Required.                                                                                                                                                   |
