# LlamaIndex Vector_Stores Integration: MongoDB
## Setting up MongoDB Atlas as the Datastore Provider
MongoDB Atlas is a multi-cloud database service made by the same people that build MongoDB.
Atlas simplifies deploying and managing your databases while offering the versatility you need
to build resilient and performant global applications on the cloud providers of your choice.
You can perform semantic search on data in your Atlas cluster running MongoDB v6.0.11, v7.0.2,
or later using Atlas Vector Search. You can store vector embeddings for any kind of data along
with other data in your collection on the Atlas cluster.
In the section, we provide detailed instructions to run the tests.
### Deploy a Cluster
Follow the [Getting-Started](https://www.mongodb.com/basics/mongodb-atlas-tutorial) documentation
to create an account, deploy an Atlas cluster, and connect to a database.
### Retrieve the URI used by Python to connect to the Cluster
When you deploy, this will be stored as the environment variable: `MONGODB_URI`,
It will look something like the following. The username and password, if not provided,
can be configured in _Database Access_ under Security in the left panel.
```
export MONGODB_URI="mongodb+srv://<username>:<password>@cluster0.foo.mongodb.net/?retryWrites=true&w=majority"
```
There are a number of ways to navigate the Atlas UI. Keep your eye out for "Connect" and "driver".
On the left panel, navigate and click 'Database' under DEPLOYMENT.
Click the Connect button that appears, then Drivers. Select Python.
(Have no concern for the version. This is the PyMongo, not Python, version.)
Once you have got the Connect Window open, you will see an instruction to `pip install pymongo`.
You will also see a **connection string**.
This is the `uri` that a `pymongo.MongoClient` uses to connect to the Database.
### Test the connection
Atlas provides a simple check. Once you have your `uri` and `pymongo` installed,
try the following in a python console.
```python
from pymongo.mongo_client import MongoClient
client = MongoClient(uri) # Create a new client and connect to the server
try:
client.admin.command(
"ping"
) # Send a ping to confirm a successful connection
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(e)
```
**Troubleshooting**
- You can edit a Database's users and passwords on the 'Database Access' page, under Security.
- Remember to add your IP address. (Try `curl -4 ifconfig.co`)
### Create a Database and Collection
As mentioned, Vector Databases provide two functions. In addition to being the data store,
they provide very efficient search based on natural language queries.
With Vector Search, one will index and query data with a powerful vector search algorithm
using "Hierarchical Navigable Small World (HNSW) graphs to find vector similarity.
The indexing runs beside the data as a separate service asynchronously.
The Search index monitors changes to the Collection that it applies to.
Subsequently, one need not upload the data first.
We will create an empty collection now, which will simplify setup in the example notebook.
Back in the UI, navigate to the Database Deployments page by clicking Database on the left panel.
Click the "Browse Collections" and then "+ Create Database" buttons.
This will open a window where you choose Database and Collection names. (No additional preferences.)
Remember these values as they will be as the environment variables,
`MONGODB_DATABASE` and `MONGODB_COLLECTION`.
### Set Datastore Environment Variables
To establish a connection to the MongoDB Cluster, Database, and Collection, plus create a Vector Search Index,
define the following environment variables.
You can confirm that the required ones have been set like this: `assert "MONGODB_URI" in os.environ`
**IMPORTANT** It is crucial that the choices are consistent between setup in Atlas and Python environment(s).
| Name | Description | Example |
| -------------------- | ----------------- | ------------------------------------------------------------------- |
| `MONGODB_URI` | Connection String | mongodb+srv://`<user>`:`<password>`@llama-index.zeatahb.mongodb.net |
| `MONGODB_DATABASE` | Database name | llama_index_test_db |
| `MONGODB_COLLECTION` | Collection name | llama_index_test_vectorstore |
| `MONGODB_INDEX` | Search index name | vector_index |
The following will be required to authenticate with OpenAI.
| Name | Description |
| ---------------- | ------------------------------------------------------------ |
| `OPENAI_API_KEY` | OpenAI token created at https://platform.openai.com/api-keys |
### Create an Atlas Vector Search Index
The final step to configure MongoDB as the Datastore is to create a Vector Search Index.
The procedure is described [here](https://www.mongodb.com/docs/atlas/atlas-vector-search/create-index/#procedure).
Under Services on the left panel, choose Atlas Search > Create Search Index >
Atlas Vector Search JSON Editor.
The Plugin expects an index definition like the following.
To begin, choose `numDimensions: 1536` along with the suggested EMBEDDING variables above.
You can experiment with these later.
```json
{
"fields": [
{
"numDimensions": 1536,
"path": "embedding",
"similarity": "cosine",
"type": "vector"
}
]
}
```
### Running MongoDB Integration Tests
In addition to the Jupyter Notebook in `examples/`,
a suite of integration tests is available to verify the MongoDB integration.
The test suite needs the cluster up and running, and the environment variables defined above.
Raw data
{
"_id": null,
"home_page": null,
"name": "llama-index-vector-stores-mongodb",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.9",
"maintainer_email": null,
"keywords": null,
"author": "The MongoDB Python Team",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/f8/b3/ab925b0ce8d6d121ec0379dc51d81b0a4ce12f521d7e634f78939f5a9cdf/llama_index_vector_stores_mongodb-0.5.0.tar.gz",
"platform": null,
"description": "# LlamaIndex Vector_Stores Integration: MongoDB\n\n## Setting up MongoDB Atlas as the Datastore Provider\n\nMongoDB Atlas is a multi-cloud database service made by the same people that build MongoDB.\nAtlas simplifies deploying and managing your databases while offering the versatility you need\nto build resilient and performant global applications on the cloud providers of your choice.\n\nYou can perform semantic search on data in your Atlas cluster running MongoDB v6.0.11, v7.0.2,\nor later using Atlas Vector Search. You can store vector embeddings for any kind of data along\nwith other data in your collection on the Atlas cluster.\n\nIn the section, we provide detailed instructions to run the tests.\n\n### Deploy a Cluster\n\nFollow the [Getting-Started](https://www.mongodb.com/basics/mongodb-atlas-tutorial) documentation\nto create an account, deploy an Atlas cluster, and connect to a database.\n\n### Retrieve the URI used by Python to connect to the Cluster\n\nWhen you deploy, this will be stored as the environment variable: `MONGODB_URI`,\nIt will look something like the following. The username and password, if not provided,\ncan be configured in _Database Access_ under Security in the left panel.\n\n```\nexport MONGODB_URI=\"mongodb+srv://<username>:<password>@cluster0.foo.mongodb.net/?retryWrites=true&w=majority\"\n```\n\nThere are a number of ways to navigate the Atlas UI. Keep your eye out for \"Connect\" and \"driver\".\n\nOn the left panel, navigate and click 'Database' under DEPLOYMENT.\nClick the Connect button that appears, then Drivers. Select Python.\n(Have no concern for the version. This is the PyMongo, not Python, version.)\nOnce you have got the Connect Window open, you will see an instruction to `pip install pymongo`.\nYou will also see a **connection string**.\nThis is the `uri` that a `pymongo.MongoClient` uses to connect to the Database.\n\n### Test the connection\n\nAtlas provides a simple check. Once you have your `uri` and `pymongo` installed,\ntry the following in a python console.\n\n```python\nfrom pymongo.mongo_client import MongoClient\n\nclient = MongoClient(uri) # Create a new client and connect to the server\ntry:\n client.admin.command(\n \"ping\"\n ) # Send a ping to confirm a successful connection\n print(\"Pinged your deployment. You successfully connected to MongoDB!\")\nexcept Exception as e:\n print(e)\n```\n\n**Troubleshooting**\n\n- You can edit a Database's users and passwords on the 'Database Access' page, under Security.\n- Remember to add your IP address. (Try `curl -4 ifconfig.co`)\n\n### Create a Database and Collection\n\nAs mentioned, Vector Databases provide two functions. In addition to being the data store,\nthey provide very efficient search based on natural language queries.\nWith Vector Search, one will index and query data with a powerful vector search algorithm\nusing \"Hierarchical Navigable Small World (HNSW) graphs to find vector similarity.\n\nThe indexing runs beside the data as a separate service asynchronously.\nThe Search index monitors changes to the Collection that it applies to.\nSubsequently, one need not upload the data first.\nWe will create an empty collection now, which will simplify setup in the example notebook.\n\nBack in the UI, navigate to the Database Deployments page by clicking Database on the left panel.\nClick the \"Browse Collections\" and then \"+ Create Database\" buttons.\nThis will open a window where you choose Database and Collection names. (No additional preferences.)\nRemember these values as they will be as the environment variables,\n`MONGODB_DATABASE` and `MONGODB_COLLECTION`.\n\n### Set Datastore Environment Variables\n\nTo establish a connection to the MongoDB Cluster, Database, and Collection, plus create a Vector Search Index,\ndefine the following environment variables.\nYou can confirm that the required ones have been set like this: `assert \"MONGODB_URI\" in os.environ`\n\n**IMPORTANT** It is crucial that the choices are consistent between setup in Atlas and Python environment(s).\n\n| Name | Description | Example |\n| -------------------- | ----------------- | ------------------------------------------------------------------- |\n| `MONGODB_URI` | Connection String | mongodb+srv://`<user>`:`<password>`@llama-index.zeatahb.mongodb.net |\n| `MONGODB_DATABASE` | Database name | llama_index_test_db |\n| `MONGODB_COLLECTION` | Collection name | llama_index_test_vectorstore |\n| `MONGODB_INDEX` | Search index name | vector_index |\n\nThe following will be required to authenticate with OpenAI.\n\n| Name | Description |\n| ---------------- | ------------------------------------------------------------ |\n| `OPENAI_API_KEY` | OpenAI token created at https://platform.openai.com/api-keys |\n\n### Create an Atlas Vector Search Index\n\nThe final step to configure MongoDB as the Datastore is to create a Vector Search Index.\nThe procedure is described [here](https://www.mongodb.com/docs/atlas/atlas-vector-search/create-index/#procedure).\n\nUnder Services on the left panel, choose Atlas Search > Create Search Index >\nAtlas Vector Search JSON Editor.\n\nThe Plugin expects an index definition like the following.\nTo begin, choose `numDimensions: 1536` along with the suggested EMBEDDING variables above.\nYou can experiment with these later.\n\n```json\n{\n \"fields\": [\n {\n \"numDimensions\": 1536,\n \"path\": \"embedding\",\n \"similarity\": \"cosine\",\n \"type\": \"vector\"\n }\n ]\n}\n```\n\n### Running MongoDB Integration Tests\n\nIn addition to the Jupyter Notebook in `examples/`,\na suite of integration tests is available to verify the MongoDB integration.\nThe test suite needs the cluster up and running, and the environment variables defined above.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "llama-index vector_stores mongodb integration",
"version": "0.5.0",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "dadbdd0ac6deafb618ec9233058076bd69abe5d89b702a9b7c2a92b8b4ebece9",
"md5": "1eea12421047d20822f355330e705ff3",
"sha256": "f1431df41866c25e7a5ba546c9fb4d73246055948d0def638fdce11589808e5a"
},
"downloads": -1,
"filename": "llama_index_vector_stores_mongodb-0.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1eea12421047d20822f355330e705ff3",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.9",
"size": 11243,
"upload_time": "2024-11-18T01:35:00",
"upload_time_iso_8601": "2024-11-18T01:35:00.011932Z",
"url": "https://files.pythonhosted.org/packages/da/db/dd0ac6deafb618ec9233058076bd69abe5d89b702a9b7c2a92b8b4ebece9/llama_index_vector_stores_mongodb-0.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "f8b3ab925b0ce8d6d121ec0379dc51d81b0a4ce12f521d7e634f78939f5a9cdf",
"md5": "dc5185a6c40b645bb0fc9d4efd56cfe9",
"sha256": "0e49559326d754b19fb311999e46cdbe69424b77ae5cfa8544bde20049476f23"
},
"downloads": -1,
"filename": "llama_index_vector_stores_mongodb-0.5.0.tar.gz",
"has_sig": false,
"md5_digest": "dc5185a6c40b645bb0fc9d4efd56cfe9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.9",
"size": 12121,
"upload_time": "2024-11-18T01:35:00",
"upload_time_iso_8601": "2024-11-18T01:35:00.872028Z",
"url": "https://files.pythonhosted.org/packages/f8/b3/ab925b0ce8d6d121ec0379dc51d81b0a4ce12f521d7e634f78939f5a9cdf/llama_index_vector_stores_mongodb-0.5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-18 01:35:00",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "llama-index-vector-stores-mongodb"
}