Docs Menu
Docs Home
/
MongoDB Atlas
/

Use MongoDB Views to Transform Documents and Filter Collections for Atlas Search

On this page

  • Requirements
  • Limitations
  • Example: Filter Documents
  • Example: Add or Modify Fields
  • Edit a View
  • Return the Pipelines for a View
  • Troubleshoot
  • Index Process
  • Learn More

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.

You must use:

  • MongoDB 8.0 or higher.

During the Preview period, you must use:

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

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

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.

1

To learn more, see Connect via mongosh.

2
use sample_mflix
3
db.createView(
"movies_ReleasedAfter2000",
"movies",
[
{
$match: {
$expr: {
$gt: [
"$released",
ISODate("2000-01-01")
]
}
}
}
]
)
4
  1. If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. If it's not already displayed, click Clusters in the sidebar.

    The Clusters page displays.

5

You can go the Atlas Search page from the sidebar, the Data Explorer, or your cluster details page.

  1. In the sidebar, click Atlas Search under the Services heading.

    Note

    If you have no clusters, click Create cluster to create one. To learn more, see Create a Cluster.

  2. From the Select data source dropdown, select your cluster and click Go to Atlas Search.

    The Atlas Search page displays.

  1. Click the Browse Collections button for your cluster.

  2. Expand the database and select the collection.

  3. Click the Search Indexes tab for the collection.

    The Atlas Search page displays.

  1. Click the cluster's name.

  2. Click the Atlas Search tab.

    The Atlas Search page displays.

6
7

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:

  • Index Name: releasedAfter2000Index

  • Database and Collection:

    • sample_mflix

    • movies_ReleasedAfter2000

Configuration Method

For a guided experience, select Visual Editor.

To edit the raw index definition, select JSON Editor.
8

Atlas displays a Toast (brief, non-interactive notification) to let you know your index is building.

9

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.

10

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
1db.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')
}
}
]

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.

1

To learn more, see Connect via mongosh.

2
use sample_airbnb
3
db.createView(
"listingsAndReviews_totalPrice",
"listingsAndReviews",
[
{
$addFields: {
totalPrice: {
$add: [
{
$ifNull: [{ $toDouble: "$price" }, 0]
},
{
$ifNull: [{ $toDouble: "$cleaning_fee" }, 0]
}
]
}
}
}
]
)
4
  1. If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. If it's not already displayed, click Clusters in the sidebar.

    The Clusters page displays.

5

You can go the Atlas Search page from the sidebar, the Data Explorer, or your cluster details page.

  1. In the sidebar, click Atlas Search under the Services heading.

    Note

    If you have no clusters, click Create cluster to create one. To learn more, see Create a Cluster.

  2. From the Select data source dropdown, select your cluster and click Go to Atlas Search.

    The Atlas Search page displays.

  1. Click the Browse Collections button for your cluster.

  2. Expand the database and select the collection.

  3. Click the Search Indexes tab for the collection.

    The Atlas Search page displays.

  1. Click the cluster's name.

  2. Click the Atlas Search tab.

    The Atlas Search page displays.

6
7

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:

  • Index Name: totalPriceIndex

  • Database and Collection:

    • sample_airbnb

    • listingsAndReviews_totalPrice

Configuration Method

For a guided experience, select Visual Editor.

To edit the raw index definition, select JSON Editor.
8
  1. Specify the following index definition:

    {
    "mappings": {
    "dynamic": true
    },
    "storedSource": {
    "include": [
    "totalPrice"
    ]
    }
    }
  2. Click Next.

9

Atlas displays a Toast (brief, non-interactive notification) to let you know your index is building.

10

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.

11

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
1db.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 }
]

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.

The following example returns the pipelines on the movies_ReleasedAfter2000 View.

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

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

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:

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

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

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.

Back

Troubleshoot