对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

将视图与MongoDB Vector Search 结合使用

You can create a MongoDB Vector Search index on a View to transform documents and collections so that you can partially index a collection, support incompatible data types or data models, and more.

The following examples use the sample_mflix sample database.

注意

消歧

This page discusses standard views. To learn about on-demand materialized views, see On-Demand Materialized Views.

To learn about the differences between the view types, see Comparison with On-Demand Materialized Views.

您必须使用 MongoDB 8.0 或更高版本。

To edit a View, you must have a User Admin role and use the collMod database command.

  • MongoDB Vector Search 支持具有以下阶段的视图:

    You can use the $expr operator only within the $match stage.

  • 索引名称在源集合及其所有视图中必须是唯一的。

  • MongoDB Vector Search doesn't support view definitions with operators that produce dynamic results, such as the $$USER_ROLES system variable and the $rand aggregation operator.

  • MongoDB Vector Search 查询会返回源集合中出现的原始文档。

To create a View, you must have the createCollection privilege.

Highly complex view transformations can increase indexing and query time. This is because the mongod must read the view definition when it filters and transforms the oplog entries during indexing (initial sync and steady state replication), and when it applies those transformations to the returned documents at query-time.

请考虑创建物化视图以避免Atlas上的额外的复制负载。您还可以直接查询源集合,以避免视图转换造成的查询延迟。

在以下场景中,索引会更改为 FAILED 状态:

  • 您在与MongoDB Vector Search 不兼容的视图上创建的索引。

  • 您编辑视图的方式不符合MongoDB Vector Search 兼容性要求。

  • 您删除或更改视图的源集合。

    例如,如果一个视图是在另一个视图上创建的,并且将父视图源更改为另一个集合。

    注意

    如果一个视图是其他视图的后代,这一限制也同样适用。例如,您无法更改或移除所有后代所源自的源集合。

在以下场景中,索引会更改为 STALE 状态:

警告

如果在视图中定义的聚合管道与集合中的文档不兼容,搜索复制将失败。例如,如果 $toDouble 表达式对包含数组的文档字段进行操作,复制将失败。确保您的视图在处理集合中的所有文档时不会出错。

  • 如果视图定义导致聚合失败,而索引为 READY,则索引变为 STALE。解析文档或更改视图定义以使其不再失败后,索引将返回到 READY。当为 STALE 时,索引仍然可查询。如果索引脱离oplog,则会触发索引重建。

  • 如果视图定义在索引为 BUILDING 时导致聚合管道失败,则在修复文档之前,则索引生成将停滞。在您解析文档或更改视图定义后,索引将返回到 READY,以确保不再失败。

You can view index statuses in the Atlas UI on the index status details page.

当您使用 8.1 之前的MongoDB版本查询视图时,会出现此错误。

  • 如果您使用 8.0 之前的MongoDB版本,我们建议您升级到 8.1+ 以直接查询视图。您可以升级到 8.0 来查询源集合。

  • 如果使用MongoDB 8.0,则必须针对源集合查询视图索引。示例,对集合而不是视图运行.aggregate()。

当您在视图上创建MongoDB Vector Search索引时,mongot进程执行的任务与您在常规集合上创建MongoDB Vector Search索引时相同。mongot进程:

  1. 根据集合索引定义中的规则创建MongoDB Vector Search 索引。

  2. 监控变更流以了解您为其定义了MongoDB Vector Search 索引的集合的文档和索引的当前状态。

  3. 处理MongoDB Vector Search 查询,并将匹配文档的文档ID 和其他搜索元数据返回给 mongod,后者执行完整文档查找并将结果返回给客户端。

当您在视图上创建MongoDB向量搜索索引时,会在步骤 1 和 2 期间应用视图定义,并且转换后的文档会根据搜索索引定义进行索引,然后存储在磁盘上。

To learn more about Views, see Views.

To create a MongoDB Search index on a View, see Use Views with MongoDB Search.