> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.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 文档数据进行原生向量搜索。

## 安装与设置

请参阅[详细配置说明](/oss/python/integrations/vectorstores/mongodb_atlas)。

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

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install langchain-mongodb
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add langchain-mongodb
  ```
</CodeGroup>

## 向量存储

请参阅[使用示例](/oss/python/integrations/vectorstores/mongodb_atlas)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_mongodb import MongoDBAtlasVectorSearch
```

## 检索器

### 全文搜索检索器

> `混合搜索检索器`使用 Lucene 的标准 (`BM25`) 分析器执行全文搜索。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_mongodb.retrievers import MongoDBAtlasFullTextSearchRetriever
```

### 混合搜索检索器

> `混合搜索检索器`结合了向量搜索和全文搜索，并通过 `倒数排名融合` (`RRF`) 算法对它们进行加权。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_mongodb.retrievers import MongoDBAtlasHybridSearchRetriever
```

## 模型缓存

### MongoDBCache

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

要导入此缓存：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_mongodb.cache import MongoDBCache
```

要将此缓存与您的 LLM 一起使用：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_core.globals import set_llm_cache

# 使用任何嵌入提供程序...
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 向量搜索索引才能工作。请查看[使用示例](/oss/python/integrations/vectorstores/mongodb_atlas)了解如何设置索引。

要导入此缓存：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_mongodb.cache import MongoDBAtlasSemanticCache
```

要将此缓存与您的 LLM 一起使用：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_core.globals import set_llm_cache

# 使用任何嵌入提供程序...
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,
))
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs)通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/providers/mongodb_atlas.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
