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

# MongoDB atlas 集成

> 使用 LangChain Python 与 MongoDB atlas 集成。

> [MongoDB Atlas](https://www.mongodb.com/docs/atlas/) 是一个可在 AWS、Azure 和 GCP 上提供的完全托管的云数据库。它现在支持 MongoDB 文档数据的原生向量搜索。

## 安装和设置

参见 [详细配置说明](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/vectorstores/mongodb_atlas.md)。

我们需要安装 `langchain-mongodb` python 包。

```bash pip theme={null}
pip install langchain-mongodb
```

```bash uv theme={null}
uv add langchain-mongodb
```

## 向量存储

参见 [使用示例](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/vectorstores/mongodb_atlas.md)。

```python theme={null}
from langchain_mongodb import MongoDBAtlasVectorSearch
```

## 检索器

参见 [使用示例](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/retrievers/mongodb_atlas.md)。

### 全文检索检索器

> `Hybrid Search Retriever` 使用 Lucene 的标准 (`BM25`) 分析器执行全文搜索。

```python theme={null}
from langchain_mongodb.retrievers import MongoDBAtlasFullTextSearchRetriever
```

### 混合搜索检索器

> `Hybrid Search Retriever` 结合向量搜索和全文搜索，通过 `Reciprocal Rank Fusion` (`RRF`) 算法进行权重分配。

```python theme={null}
from langchain_mongodb.retrievers import MongoDBAtlasHybridSearchRetriever
```

## 模型缓存

### MongoDBCache

一个用于在 MongoDB 中存储简单缓存的抽象。这不会使用语义缓存，生成之前也不需要在集合上创建索引。

要导入此缓存：

```python theme={null}
from langchain_mongodb.cache import MongoDBCache
```

要在 LLMs 中使用此缓存：

```python theme={null}
from langchain_core.globals import set_llm_cache

# use any embedding provider...
from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings

mongodb_atlas_uri = "<YOUR_CONNECTION_STRING>"
COLLECTION_NAME="<YOUR_CACHE_COLLECTION_NAME>"
DATABASE_NAME="<YOUR_DATABASE_NAME>"

set_llm_cache(MongoDBCache(
    connection_string=mongodb_atlas_uri,
    collection_name=COLLECTION_NAME,
    database_name=DATABASE_NAME,
))
```

### MongoDBAtlasSemanticCache

语义缓存允许用户根据用户输入与先前缓存结果之间的语义相似性检索缓存的提示词。在底层，它将 MongoDBAtlas 同时用作缓存和向量存储。
MongoDBAtlasSemanticCache 继承自 `MongoDBAtlasVectorSearch`，工作需要定义 Atlas 向量搜索索引。请查看 [使用示例](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/vectorstores/mongodb_atlas.md) 了解如何设置索引。

要导入此缓存：

```python theme={null}
from langchain_mongodb.cache import MongoDBAtlasSemanticCache
```

要在 LLMs 中使用此缓存：

```python theme={null}
from langchain_core.globals import set_llm_cache

# use any embedding provider...
from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings

mongodb_atlas_uri = "<YOUR_CONNECTION_STRING>"
COLLECTION_NAME="<YOUR_CACHE_COLLECTION_NAME>"
DATABASE_NAME="<YOUR_DATABASE_NAME>"

set_llm_cache(MongoDBAtlasSemanticCache(
    embedding=FakeEmbeddings(),
    connection_string=mongodb_atlas_uri,
    collection_name=COLLECTION_NAME,
    database_name=DATABASE_NAME,
))
```

***

> 官方原文：[src/oss/python/integrations/providers/mongodb\_atlas.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/python/integrations/providers/mongodb_atlas.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.