> ## Documentation Index
> Fetch the complete documentation index at: https://javaai.pig4cloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# MongoDB 向量数据库

Spring AI 中的 MongoDB 向量数据库集成了 MongoDB 的向量功能，提供向量存储和搜索能力。

## 概述

MongoDB 向量数据库支持：

* 基于文档的向量存储
* 灵活的模式设计
* 实时向量搜索
* 可扩展架构

## 特性

<CardGroup cols={2}>
  <Card title="基于文档" icon="file-alt">
    文档向量存储
  </Card>

  <Card title="灵活" icon="sliders-h">
    灵活的模式设计
  </Card>

  <Card title="实时" icon="clock">
    实时搜索
  </Card>

  <Card title="可扩展" icon="arrows-up-down-left-right">
    水平扩展
  </Card>
</CardGroup>

## 实现

### 基本设置

```xml theme={"system"}
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-mongodb-vectordb</artifactId>
    <version>${spring-ai.version}</version>
</dependency>
```

### 配置

```properties theme={"system"}
# MongoDB 向量数据库配置
spring.ai.mongodb.vectordb.enabled=true
spring.ai.mongodb.vectordb.uri=mongodb://localhost:27017
spring.ai.mongodb.vectordb.database=vectordb
spring.ai.mongodb.vectordb.collection=vectors
```

### 用法

```java theme={"system"}
@Service
public class MongoDBVectorService {
    private final MongoDBVectorClient vectorClient;

    public MongoDBVectorService(MongoDBVectorClient vectorClient) {
        this.vectorClient = vectorClient;
    }

    public void storeVector(String id, float[] vector, Map<String, Object> metadata) {
        vectorClient.store(id, vector, metadata);
    }

    public List<SearchResult> searchSimilar(float[] queryVector, int k) {
        return vectorClient.search(queryVector, k);
    }
}
```

## 向量操作

### 1. 向量存储

```java theme={"system"}
@Configuration
public class MongoDBVectorStorageConfig {
    @Bean
    public VectorStorage mongoDBVectorStorage(MongoDBVectorProperties properties) {
        return new MongoDBVectorStorage(properties);
    }
}
```

### 2. 相似性搜索

```java theme={"system"}
@Configuration
public class MongoDBSimilaritySearchConfig {
    @Bean
    public SimilaritySearch mongoDBSimilaritySearch(MongoDBVectorProperties properties) {
        return new MongoDBSimilaritySearch(properties);
    }
}
```

### 3. 集合管理

```java theme={"system"}
@Service
public class CollectionService {
    private final MongoDBVectorClient vectorClient;

    public void createCollection(String name, int dimension) {
        vectorClient.createCollection(name, dimension);
    }

    public void dropCollection(String name) {
        vectorClient.dropCollection(name);
    }
}
```

## 高级特性

### 自定义集合配置

```java theme={"system"}
@Configuration
public class CollectionConfig {
    @Bean
    public CollectionConfig collectionConfig() {
        return CollectionConfig.builder()
            .name("custom-vectors")
            .dimension(1536)
            .indexType(IndexType.HNSW)
            .metricType(MetricType.COSINE)
            .build();
    }
}
```

### 索引管理

```java theme={"system"}
@Configuration
public class IndexConfig {
    @Bean
    public IndexConfig indexConfig() {
        return IndexConfig.builder()
            .name("vector_index")
            .type(IndexType.HNSW)
            .dimension(1536)
            .build();
    }
}
```

### 监控

```properties theme={"system"}
management.endpoints.web.exposure.include=mongodb-vectordb
management.endpoint.mongodb-vectordb.enabled=true
```

## 最佳实践

<Note>
  使用 MongoDB 向量数据库时，请考虑以下最佳实践：
</Note>

* **集合设计**：设计高效的集合结构
* **索引管理**：优化索引配置
* **分片策略**：实施有效的分片
* **资源管理**：配置资源分配
* **监控**：设置全面的监控

## 故障排除

常见问题及解决方案：

1. **连接问题**
   * 验证连接 URI
   * 检查身份验证
   * 查看网络设置

2. **性能问题**
   * 优化索引配置
   * 调整分片策略
   * 查看查询模式

3. **存储问题**
   * 监控集合大小
   * 执行清理
   * 查看保留策略

<Card title="文档有误？请协助编辑" icon="pencil" href="https://github.com/javaai-pig4cloud-com/javaai-docs/edit/main/spring-ai/api/vectordbs/mongodb.mdx">
  发现文档问题？点击此处直接在 GitHub 上编辑并提交 PR，帮助我们改进文档！
</Card>
