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 或更高版本。
在 MongoDB v8.0+ 上:
使用Atlas用户界面或Atlas管理API在视图上创建MongoDB Vector Search 索引。
针对源集合运行MongoDB Vector Search 查询。参考在视图上创建的MongoDB Vector Search索引。 这些查询返回源集合中出现的原始文档。
在 MongoDB v8.1+ 上,您还可以:
Use
mongoshand Driver methods,db.collection.createSearchIndex(),db.collection.updateSearchIndex(),db.collection.dropSearchIndex(), and$listSearchIndexesto create and manage MongoDB Vector Search indexes on Views.针对该视图运行MongoDB Vector Search 查询。
在 MongoDB v8.2+ 上,您还可以:
Use
$rankFusionin aggregation pipelines on Views.Query Views that include sharded sub-pipelines with
$lookupand$unionWith.
To edit a View, you must have a User Admin role and use the collMod database command.
限制
索引名称在源集合及其所有视图中必须是唯一的。
MongoDB Vector Search doesn't support view definitions with operators that produce dynamic results, such as the $$USER_ROLES system variable and the
$randaggregation 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
在以下场景中,索引会更改为 FAILED 状态:
您在与MongoDB Vector Search 不兼容的视图上创建的索引。
您编辑视图的方式不符合MongoDB Vector Search 兼容性要求。
您删除或更改视图的源集合。
例如,如果一个视图是在另一个视图上创建的,并且将父视图源更改为另一个集合。
注意
如果一个视图是其他视图的后代,这一限制也同样适用。例如,您无法更改或移除所有后代所源自的源集合。
索引更改为 STALE
在以下场景中,索引会更改为 STALE 状态:
警告
如果在视图中定义的聚合管道与集合中的文档不兼容,搜索复制将失败。例如,如果 $toDouble 表达式对包含数组的文档字段进行操作,复制将失败。确保您的视图在处理集合中的所有文档时不会出错。
如果视图定义导致聚合失败,而索引为
READY,则索引变为STALE。解析文档或更改视图定义以使其不再失败后,索引将返回到READY。当为STALE时,索引仍然可查询。如果索引脱离oplog,则会触发索引重建。如果视图定义在索引为
BUILDING时导致聚合管道失败,则在修复文档之前,则索引生成将停滞。在您解析文档或更改视图定义后,索引将返回到READY,以确保不再失败。
You can view index statuses in the Atlas UI on the index status details page.
错误:$search 仅作为管道中的第一阶段有效
当您使用 8.1 之前的MongoDB版本查询视图时,会出现此错误。
如果您使用 8.0 之前的MongoDB版本,我们建议您升级到 8.1+ 以直接查询视图。您可以升级到 8.0 来查询源集合。
如果使用MongoDB 8.0,则必须针对源集合查询视图索引。示例,对集合而不是视图运行
.aggregate()。
索引流程
当您在视图上创建MongoDB Vector Search索引时,mongot进程执行的任务与您在常规集合上创建MongoDB Vector Search索引时相同。mongot进程:
根据集合索引定义中的规则创建MongoDB Vector Search 索引。
监控变更流以了解您为其定义了MongoDB Vector Search 索引的集合的文档和索引的当前状态。
处理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.