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.
Note
Disambiguation
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.
Requirements
You must use MongoDB 8.0 or higher.
On MongoDB v8.0+:
Use the Atlas UI or the Atlas Administration API to create MongoDB Vector Search indexes on Views.
Run the MongoDB Vector Search queries against the source collection. Reference the MongoDB Vector Search index that was created on the View. These queries return the original documents as they appear in the source collection.
On MongoDB v8.1+, you can additionally:
Use
mongoshand Driver methods,db.collection.createSearchIndex(),db.collection.updateSearchIndex(),db.collection.dropSearchIndex(), and$listSearchIndexesto create and manage MongoDB Vector Search indexes on Views.Run the MongoDB Vector Search queries against the View.
To edit a View, you must have a User Admin
role and use the collMod database command.
Limitations
MongoDB Vector Search supports Views with the following stages:
Index names must be unique across a source collection and all of its Views.
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 queries return the original documents as they appear in the source collection.
Example: Filter Documents
To create a View, you must have the
createCollection privilege.
You can filter documents to partially index a collection. The
following example creates a View on the
sample_mflix.embedded_movies collection so that only documents
that have a vector embedding field are indexed.
Performance Considerations
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.
Consider creating a materialized view to avoid extra replication load on Atlas. You can also query the source collection directly to avoid query latency from the view transformation.
Troubleshoot
Indexes Change to FAILED
Indexes change to the FAILED status in the following scenarios:
You create an index on a View that is incompatible with MongoDB Vector Search.
You edit a View in a way that does not meet the MongoDB Vector Search compatibility requirements.
You remove or change a View's source collection.
For example, if one View is created on another View, and you change the parent View source to another collection.
Note
This limitation also applies if a View is a descendent of other Views. For example, you can't change or remove the source collection that all descendents originate from.
Indexes Change to STALE
Indexes change to the STALE status in the following scenarios:
Warning
If the aggregation pipeline defined in your View is incompatible
with the documents in the collection, search replication fails.
For example, if a $toDouble expression operates on a document
field that contains an array, the replication fails. Ensure your
View works with all documents in the collection without errors.
If the View definition causes an aggregation failure while an index is
READY, the index becomesSTALE. The index will return toREADYafter you resolve the document or change the view definition so that it doesn't fail anymore. WhenSTALE, the index remains queryable. If the index falls off the oplog, an index rebuild is triggered.If the View definition causes an aggregation pipeline failure while the index is
BUILDING, the index build is stuck until you fix the document. The index will return toREADYafter you resolve the document or change the view definition so that it doesn't fail anymore.
You can view index statuses in the Atlas UI on the index status details page.
Error: $search is only valid as the first stage in a pipeline
This error appears when you query a view using a MongoDB version before 8.1.
If you use a MongoDB version before 8.0, we recommend you upgrade to 8.1+ to query the view directly. You can upgrade to 8.0 to query the source collection.
If you use MongoDB 8.0, you must query the view index against the source collection. For example, run
.aggregate()on the collection instead of the view.
Index Process
When you create an MongoDB Vector Search index on a View, the mongot
process performs the same tasks as when you create an MongoDB Vector Search
index on a regular collection. The mongot process:
Creates MongoDB Vector Search indexes based on the rules in the index definition for the collection.
Monitors change streams for the current state of the documents and indexes for the collections for which you defined the MongoDB Vector Search indexes.
Processes MongoDB Vector Search queries and returns the document IDs and other search metadata for the matching documents to
mongod, which then does a full document lookup and returns the results to the client.
When you create an MongoDB Vector Search index on a View, the View definition is applied during Step 1 and 2, and the transformed documents are indexed based on the search index definition, then stored on disk.
Learn More
To learn more about Views, see Views.
To create a MongoDB Search index on a View, see Use Views with MongoDB Search.