Use MongoDB Views to Transform Documents and Filter Collections for Atlas Search
On this page
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.
Note
Using MongoDB Views with Atlas Search is available as a Preview feature. The feature and the corresponding documentation might change at any time during the Preview period.
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.
During the Preview period, you must use:
Atlas UI and the Atlas Administration API to create Atlas Search indexes on Views. Support for
mongosh
, Compass, and Drivers will be available when this feature is Generally Available.
To edit a View, you must have a User Admin
role and use the collMod
database command.
Limitations
Atlas Search supports Views only for
$expr
in 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 variables that produce dynamic results like
$$USER_ROLES
and$random
.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.
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. |
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 source
collection movies
. During the Preview period,
you must query search indexes created on Views using the
source collection (movies
) of the View.
If you query the View (movies_ReleasedAfter2000
),
Atlas Search returns no results.
use sample_mflix
1 db.movies.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.
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. |
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 source
collection listingsAndReviews
. During the Preview period,
you must query search indexes created on Views using the
source collection (listingsAndReviews
) of the View.
If you query the View (listingsAndReviews_totalPrice
),
Atlas Search returns no results.
use sample_airbnb
1 db.listingsAndReviews.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') ] } } } ]
Troubleshoot
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 stall 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 toREADY
after you resolve the document or change the view definition so that it doesn't fail anymore. However, the index is queryable until the replication is automatically removed from the oplog.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 toREADY
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.
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 MongoDB Views to Transform Documents and Filter Collections for Atlas Vector Search.