Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /

Use Views with 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.

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.

You must use MongoDB 8.0 or higher.

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

  • 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.

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.

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.

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 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 becomes STALE. The index will return to READY after you resolve the document or change the view definition so that it doesn't fail anymore. When STALE, 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 to READY after 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.

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.

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:

  1. Creates MongoDB Vector Search indexes based on the rules in the index definition for the collection.

  2. Monitors change streams for the current state of the documents and indexes for the collections for which you defined the MongoDB Vector Search indexes.

  3. 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.

To learn more about Views, see Views.

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

Back

Explain Query Results

Earn a Skill Badge

Master "Vector Search Fundamentals" for free!

Learn more

On this page