# Mongo DB

#### Integrating MongoDB with Qyver

This guide provides step-by-step instructions on configuring and integrating MongoDB with Qyver.

**Configuring an Existing Managed MongoDB Instance**

To use MongoDB with Qyver, ensure your MongoDB version supports **Atlas Vector Search**. Refer to the [official MongoDB documentation](https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-overview/) for more details.

Qyver requires access to MongoDB to manage **Atlas Search Indexes**. MongoDB’s instance size affects how these indexes can be created, listed, and deleted. Specifically:

* Instances below **M10** do not allow these operations via a standard user and require access through the **administration API**.
* Larger instances allow direct index management.

To support all instance types, Qyver uses the **administration API** for index management. This requires an **API key** with the **Project Data Access Admin** role. Instructions for creating this key are included below.

**Note:** The API requires both a **project ID** and **cluster name**. Instructions on retrieving this information are also provided.

***

#### Configuration Modifications

To integrate MongoDB, add the `MongoDBVectorDatabase` class and include it in the executor:

```python
from qyver import framework as qv

vector_database = qv.MongoDBVectorDatabase(
    host="<USER>:<PASSWORD>@<HOST_URL>",  # MongoDB connection string, including username and password
    db_name="<DATABASE_NAME>",  # Name of the database (must be created manually)
    cluster_name="<CLUSTER_NAME>",  # Name of the cluster in MongoDB Atlas
    project_id="<PROJECT_ID>",  # Project ID (not the project name)
    admin_api_user="<API_USER>",  # API key user (public key in MongoDB Atlas)
    admin_api_password="<API_PASSWORD>",  # API password (private key in MongoDB Atlas)
    default_query_limit=10,  # (Optional) Maximum number of results per query (defaults to 10)
    # Additional parameters can be passed as kwargs and will be handled by the MongoDB client.
)
```

**Finding Your Project ID**

* In MongoDB Atlas, select your **organization** from the top left corner.
* Find your **project** but do not click on it.
* Click the **Actions (···)** menu in the last column and select **Copy Project ID**.

Alternatively, your **Project ID** is in the URL when viewing a project:\
`https://cloud.mongodb.com/v2/<PROJECT_ID>#/overview`\
Make sure to copy the **correct ID** and not the organization ID.

**Setting Up the Executor**

Once the MongoDBVectorDatabase is configured, assign it to the executor:

```python
...
executor = qv.RestExecutor(
    sources=[source],
    indices=[index],
    queries=[qv.RestQuery(qv.RestDescriptor("query"), query)],
    vector_database=vector_database,  # Use the MongoDBVectorDatabase instance
)
...
```

***

#### Starting a Managed MongoDB Instance

Follow these steps to set up a **MongoDB Atlas** instance:

1. **Create the Database**
   * Sign in to MongoDB Atlas.
   * Create a cluster and note the **cluster name**.
   * Click **Database** from the left menu.
   * Once the cluster is created, open it and navigate to the **Collections** tab.
   * Click **Add My Own Data**, enter a database name, and create a collection.
2. **Create a Database User and Allow IP Access**
   * Click **Database** from the left menu.
   * Click **Connect** next to your cluster.
   * In the pop-up, allow access from **any IP** or specify your machine’s IP.
   * Create a **database user** with a username and password.
3. **Generate an API Key**
   * Go to **Access Manager** and select your **project**.
   * Open the **API Keys** tab.
   * Create an API key with the **Project Data Access Admin** role.
   * Save the **private key** (it will not be accessible again).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://qyverlabs.gitbook.io/qyverlabs-docs/run-in-production/supported-vector-databases/mongo-db.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
