You can create an Atlas 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 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.
Requirements
You must use MongoDB 8.0 or higher.
On MongoDB v8.0+:
Use the Atlas UI or the Atlas Administration API to create Atlas Search indexes on Views.
Run the Atlas Search queries against the source collection. Reference the Atlas 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 Atlas Search indexes on Views.Run the Atlas Search queries against the View.
To edit a View, you must have a User Admin
role and use the collMod database command.
Limitations
Atlas Search supports Views only for
$exprin the following stages:Index names must be unique across a source collection and all of its Views.
Atlas Search doesn't support view definitions with operators that produce dynamic results, such as the $$USER_ROLES system variable and the $rand aggregation operator.
Atlas 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 partially index a collection to filter documents. The following
example creates a View on the sample_mflix.movies collection so
that you can search for only movies released after January 1,
2000.
Connect to the Atlas cluster using mongosh.
To learn more, see Connect via mongosh.
In Atlas, go to the Clusters page for your project.
WARNING: Navigation Improvements In Progress We're currently rolling out a new and improved navigation experience. If the following steps don't match your view in the Atlas UI, see the preview documentation.
If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it's not already displayed, select your desired project from the Projects menu in the navigation bar.
If it's not already displayed, click Clusters in the sidebar.
The Clusters page displays.
Start your index configuration.
Make the following selections on the page and then click Next.
Search Type | Select the Atlas Search index type. |
Index Name and Data Source | Specify the following information:
|
Configuration Method | For a guided experience, select Visual Editor. To edit the raw index definition, select JSON Editor. |
Note
Your Atlas Search index is named default by default. If you keep this name, then your index will be the
default Search index for any Atlas Search query that does not specify a different index option in
its operators. If you are creating multiple indexes, we recommend
that you maintain a consistent, descriptive naming convention across your indexes.
Check the status.
The newly created index appears on the Atlas Search tab. While the index is building, the Status field reads Build in Progress. When the index is finished building, the Status field reads Active.
Note
Larger collections take longer to index. You will receive an email notification when your index is finished building.
Run a query on the releasedAfter2000Index partial index.
Note
The following example queries the releasedAfter2000Index index by
running the .aggregate command against the View named
movies_ReleasedAfter2000. If your cluster is running MongoDB v8.0, you must query
the source collection (for example, movies) using the
index on the View. Upgrade to MongoDB v8.1+ to query the View directly.
use sample_mflix
1 db.db.movies_ReleasedAfter2000.aggregate([ 2 { 3 $search: { 4 index: "releasedAfter2000Index", 5 text: { 6 path: "title", 7 query: "foo" 8 }, 9 sort: { 10 released: 1 11 } 12 } 13 } 14 ])
[ { _id: ObjectId('573a13d2f29313caabd929f8'), plot: "Rising from the ashes of Nirvana, the Foo Fighters became a Grammy-winning sensation on their own. Sixteen years of the band's history comes to life in this documentary, from their demo ...", genres: [ 'Documentary', 'Music' ], runtime: 150, cast: [ 'Shawn Cloninger', 'William Goldsmith', 'Jessy Greene', 'Dave Grohl' ], num_mflix_comments: 0, poster: 'https://m.media-amazon.com/images/M/MV5BMzE4OTczMTgxM15BMl5BanBnXkFtZTcwNTU1NjQxOA@@._V1_SY1000_SX677_AL_.jpg', title: 'Foo Fighters: Back and Forth', fullplot: `Rising from the ashes of Nirvana, the Foo Fighters became a Grammy-winning sensation on their own. Sixteen years of the band's history comes to life in this documentary, from their demo tapes through the creation of their 2011 album, "Wasting Light."`, languages: [ 'English' ], released: ISODate('2011-04-05T00:00:00.000Z'), directors: [ 'James Moll' ], awards: { wins: 1, nominations: 1, text: '1 win & 1 nomination.' }, lastupdated: '2015-08-19 00:00:25.937000000', year: 2011, imdb: { rating: 8.4, votes: 3745, id: 1853563 }, countries: [ 'USA' ], type: 'movie', tomatoes: { viewer: { rating: 4.4, numReviews: 857, meter: 96 }, dvd: ISODate('2011-08-08T00:00:00.000Z'), website: 'http://us.foofightersfilm.com/', production: 'Cinedigm Digital Cinema', lastUpdated: ISODate('2015-09-12T18:42:01.000Z') } } ]
Example: Add or Modify Fields
The following example lets you search the
sample_airbnb.listingsAndReviews collection for accomodatations
based on a new totalPrice field, which is the sum of the price
and cleaningFee fields. Also, since Atlas Search doesn't support
Decimal128 types, we transform the values to Double.
Connect to the Atlas cluster using mongosh.
To learn more, see Connect via mongosh.
In Atlas, go to the Clusters page for your project.
WARNING: Navigation Improvements In Progress We're currently rolling out a new and improved navigation experience. If the following steps don't match your view in the Atlas UI, see the preview documentation.
If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it's not already displayed, select your desired project from the Projects menu in the navigation bar.
If it's not already displayed, click Clusters in the sidebar.
The Clusters page displays.
Start your index configuration.
Make the following selections on the page and then click Next.
Search Type | Select the Atlas Search index type. |
Index Name and Data Source | Specify the following information:
|
Configuration Method | For a guided experience, select Visual Editor. To edit the raw index definition, select JSON Editor. |
Note
Your Atlas Search index is named default by default. If you keep this name, then your index will be the
default Search index for any Atlas Search query that does not specify a different index option in
its operators. If you are creating multiple indexes, we recommend
that you maintain a consistent, descriptive naming convention across your indexes.
Check the status.
The newly created index appears on the Atlas Search tab. While the index is building, the Status field reads Build in Progress. When the index is finished building, the Status field reads Active.
Note
Larger collections take longer to index. You will receive an email notification when your index is finished building.
Run a query on the totalPriceIndex index.
Note
The following example queries the totalPriceIndex index by
running the .aggregate command against the View named
listingsAndReviews_totalPrice. If your cluster is running MongoDB v8.0, you must query
the source collection (for example, listingsAndReviews) using the
index on the View. Upgrade to MongoDB v8.1+ to query the View directly.
use sample_airbnb
1 db.listingsAndReviews_totalPrice.aggregate([ 2 { 3 $search: { 4 index: "totalPriceIndex", 5 range: { 6 path: "totalPrice", 7 lte: 300 8 }, 9 returnStoredSource: true 10 } 11 } 12 ])
[ { _id: '10006546', totalPrice: 115 }, { _id: '1001265', totalPrice: 215 }, { _id: '10021707', totalPrice: 40 }, { _id: '1003530', totalPrice: 270 }, { _id: '10038496', totalPrice: 269 }, { _id: '10051164', totalPrice: 250 }, { _id: '10057447', totalPrice: 50 }, { _id: '10057826', totalPrice: 205 }, { _id: '10059244', totalPrice: 43 }, { _id: '10066928', totalPrice: 140 }, { _id: '10082422', totalPrice: 60 }, { _id: '10083468', totalPrice: 40 }, { _id: '10084023', totalPrice: 231 }, { _id: '10091713', totalPrice: 231 }, { _id: '10092679', totalPrice: 58 }, { _id: '10096773', totalPrice: 205 }, { _id: '10112159', totalPrice: 90 }, { _id: '10117617', totalPrice: 55 }, { _id: '10120414', totalPrice: 150 }, { _id: '10133554', totalPrice: 121 } ]
Edit a View
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, Atlas Search automatically detects the change in the View definition and performs a reindexing with no downtime.
Return the Pipelines for a View
The following example returns the pipelines on the
movies_ReleasedAfter2000 View.
1 db.getCollectionInfos({ name: "movies_ReleasedAfter2000" })[0].options.pipeline
[ { '$match': { '$expr': { '$gt': [ '$released', ISODate('2000-01-01T00:00:00.000Z') ] } } } ]
Performance Considerations
For indexes created on views, Atlas Search applies the transformations or filters that you define in the view pipeline at query-time and at replication-time. While trivial transformations or filters in the view pipeline typically result in negligible performance impact, complex transformations or highly selective filters in the view pipeline might significantly slow down querying or replication. If you want to perform a heavy transformation on the documents or apply a highly selective filter, consider creating a materialized view with already transformed or filtered documents. You can evaluate view performance by comparing query latency executed against the view and against its source collection.
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 Atlas Search.
You edit a View in a way that does not meet the Atlas 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 Atlas Search index on a View, the mongot
process performs the same tasks as when you create an Atlas Search
index on a regular collection. The mongot process:
Creates Atlas 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 Atlas Search indexes.
Processes Atlas 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 Atlas Search index on a View, the View definition is applied during Step 1 and 2, and the transformed documents are stored in the Atlas Search index on disk.
Learn More
To learn more about Views, see Views.
To create an Atlas Vector Search index on a View, see Use Views with Atlas Vector Search.