Docs Menu
Docs Home
/ /

Use Views with MongoDB Search

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

The following examples use the sample_mflix and sample_airbnb sample databases.

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 Search supports Views with the following stages:

  • Index names must be unique across a source collection and all of its Views.

  • MongoDB 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 Search queries return the original documents as they appear in the source collection.

  • To retrieve the transformed document, use the storedSource option.

To create a View, your role must have the createCollection privilege.

The following examples demonstrate how to create a View, partially index documents, and run queries against the view using the index.

The following example updates the movies_ReleasedAfter2000 MongoDB View for movies before 2000.

db.runCommand(
{
collMod: "movies_ReleasedAfter2000",
viewOn: "movies",
"pipeline": [
{
$match: {
$expr: {
$lt: [
"$released",
ISODate("2000-01-01T00")
]
}
}
}
]
}
)

After you run this command, MongoDB Search automatically detects the change in the View definition and performs reindexing with no downtime.

The following example returns the pipelines on the movies_ReleasedAfter2000 View.

db.getCollectionInfos({ name: "movies_ReleasedAfter2000" })[0].options.pipeline
[
{
'$match': {
'$expr': { '$gt': [ '$released', ISODate('2000-01-01T00:00:00.000Z') ] }
}
}
]

Highly complex view transformations can lead to slower performance when Atlas reads the view to filter and transform the source collection. In this scenario, consider creating a materialized view to avoid additional replication load on Atlas. To avoid query latency caused by the view transformation, you can query the source collection directly to retrieve the original documents.

Indexes change to the FAILED status in the following scenarios:

  • You create an index on a View that is incompatible with MongoDB Search.

  • You edit a View in a way that does not meet the MongoDB 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 Search index on a View, the mongot process performs the same tasks as when you create an MongoDB Search index on a regular collection. The mongot process:

  1. Creates MongoDB 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 Search indexes.

  3. Processes MongoDB 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 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 Vector Search index on a View, see Use Views with MongoDB Vector Search.

Back

Multiple Collections

On this page