Docs Menu

Docs HomeLaunch & Manage MongoDBMongoDB Atlas

How to Run Atlas Search Queries Against Objects in Arrays

On this page

  • Overview
  • About the Sample Collection
  • About the Atlas Search Index
  • About the Queries
  • Try it in the Atlas Search Playground
  • Access the Atlas Search Playground.
  • Review the Data Source, Index, and Query panes.
  • Click Run or press Command + Enter to run the query.
  • Access the Atlas Search Playground.
  • Review the Data Source, Index, and Query panes.
  • Click Run or press Command + Enter to run the query.
  • Access the Atlas Search Playground.
  • Review the Data Source, Index, and Query panes.
  • Click Run or press Command + Enter to run the query.
  • Try it on Your Atlas Cluster
  • Create a Sample Collection and Load the Data
  • Create an Atlas Search Index
  • Run $search Queries Against Embedded Document Fields
  • Run $searchMeta Query Against Embedded Document Fields

This tutorial describes how to index and run Atlas Search queries against fields in documents, or objects, that are inside an array (embeddedDocuments). The page contains instructions for running sample queries using a sample index for a sample collection that we have set up for you in the Atlas Search Playground or that you can load, configure, and run on your Atlas cluster.

The sample collection is named schools and it contains three documents. Each document in the sample collection contains the name and mascot of the school, school teachers' first and last names, the classes that each teacher teaches including the subject name and grade level, and the various clubs for the school students.

The index definition for the collection shows the following:

  • Documents in the arrays at the teachers and teachers.classes paths are indexed as embeddedDocuments, and the fields inside the documents are dynamically indexed.

  • Documents in the arrays at the teachers path are also indexed as the document type to support highlighting, and the fields inside the documents are dynamically indexed.

  • Document in the clubs field is indexed as the document type with dynamic mappings enabled and the arrays of documents in the clubs.sports field are indexed as the embeddedDocuments type with dynamic mappings enabled.

The sample queries search the embedded documents in the schools collection. The queries use the following pipeline stages:

  • $search to search the collection.

  • $project to include and exclude fields from the collection, and add a field named score in the results. For queries that enable highlighting, the $project stage also adds a new field called highlights, which contains the highlighting information.

The tutorial demonstrates three different queries.

On the Atlas Search Playground, we have set up an embedded documents collection, pre-configured an index for the fields in the collection, and defined a query that you can run against the collection. You can also modify the collection, index, and query in the Atlas Search Playground.

To demonstrate how to run queries against embedded documents, this section walks you through the following steps:

  1. Create a sample collection named schools with embedded documents in your Atlas cluster.

  2. Set up an Atlas Search index with embeddedDocuments fields configured at the following paths:

    • teachers field

    • teachers.classes field

    • clubs.sports field

  3. Run $search queries that search the embedded documents in the schools collection using the compound operator with the embeddedDocument and text operators.

  4. Run a $searchMeta query against an embedded document field to get a count.

Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites. For this tutorial, you don't need to upload the sample data because you will create a new collection and load the documents that you need to run the queries in this tutorial.

You must begin by creating a collection named schools in an existing or new database on your Atlas cluster. After creating the collection, you must upload the sample data into your collection. To learn more about the documents in the sample collection, see About the Sample Collection.

The steps in this section walk you through creating a new database and collection, and loading the sample data into your collection.

1
  1. Log in to your cluster.

  2. Click your cluster name.

  3. Click Collections.

2
  1. Click Create Database to create a new database.

  2. Enter the database name and collection name.

    • In the Database Name field, specify local_school_district.

    • For the Collection Name field, specify schools.

3
  1. Select the schools collection if it's not selected.

  2. Click Insert Document for each of the sample documents to add to the collection.

  3. Click the JSON view ({}) to replace the default document.

  4. Copy and paste the following sample documents, one at a time, and click Insert to add the documents, one at a time, to the collection.

    {
    "_id": 0,
    "name": "Springfield High",
    "mascot": "Pumas",
    "teachers": [{
    "first": "Jane",
    "last": "Smith",
    "classes": [{
    "subject": "art of science",
    "grade": "12th"
    },
    {
    "subject": "applied science and practical science",
    "grade": "9th"
    },
    {
    "subject": "remedial math",
    "grade": "12th"
    },
    {
    "subject": "science",
    "grade": "10th"
    }]
    },
    {
    "first": "Bob",
    "last": "Green",
    "classes": [{
    "subject": "science of art",
    "grade": "11th"
    },
    {
    "subject": "art art art",
    "grade": "10th"
    }]
    }],
    "clubs": {
    "stem": [
    {
    "club_name": "chess",
    "description": "provides students opportunity to play the board game of chess informally and competitively in tournaments."
    },
    {
    "club_name": "kaboom chemistry",
    "description": "provides students opportunity to experiment with chemistry that fizzes and explodes."
    }
    ],
    "arts": [
    {
    "club_name": "anime",
    "description": "provides students an opportunity to discuss, show, and collaborate on anime and broaden their Japanese cultural understanding."
    },
    {
    "club_name": "visual arts",
    "description": "provides students an opportunity to train, experiment, and prepare for internships and jobs as photographers, illustrators, graphic designers, and more."
    }
    ]
    }
    }
    {
    "_id": 1,
    "name": "Evergreen High",
    "mascot": "Jaguars",
    "teachers": [{
    "first": "Jane",
    "last": "Earwhacker",
    "classes": [{
    "subject": "art",
    "grade": "9th"
    },
    {
    "subject": "science",
    "grade": "12th"
    }]
    },
    {
    "first": "John",
    "last": "Smith",
    "classes": [{
    "subject": "math",
    "grade": "12th"
    },
    {
    "subject": "art",
    "grade": "10th"
    }]
    }],
    "clubs": {
    "sports": [
    {
    "club_name": "archery",
    "description": "provides students an opportunity to practice and hone the skill of using a bow to shoot arrows in a fun and safe environment."
    },
    {
    "club_name": "ultimate frisbee",
    "description": "provides students an opportunity to play frisbee and learn the basics of holding the disc and complete passes."
    }
    ],
    "stem": [
    {
    "club_name": "zapped",
    "description": "provides students an opportunity to make exciting gadgets and explore electricity."
    },
    {
    "club_name": "loose in the chem lab",
    "description": "provides students an opportunity to put the scientific method to the test and get elbow deep in chemistry."
    }
    ]
    }
    }
    {
    "_id": 2,
    "name": "Lincoln High",
    "mascot": "Sharks",
    "teachers": [{
    "first": "Jane",
    "last": "Smith",
    "classes": [{
    "subject": "science",
    "grade": "9th"
    },
    {
    "subject": "math",
    "grade": "12th"
    }]
    },
    {
    "first": "John",
    "last": "Redman",
    "classes": [{
    "subject": "art",
    "grade": "12th"
    }]
    }],
    "clubs": {
    "arts": [
    {
    "club_name": "ceramics",
    "description": "provides students an opportunity to acquire knowledge of form, volume, and space relationships by constructing hand-built and wheel-thrown forms of clay."
    },
    {
    "club_name": "digital art",
    "description": "provides students an opportunity to learn about design for entertainment, 3D animation, technical art, or 3D modeling."
    }
    ],
    "sports": [
    {
    "club_name": "dodgeball",
    "description": "provides students an opportunity to play dodgeball by throwing balls to eliminate the members of the opposing team while avoiding being hit themselves."
    },
    {
    "club_name": "martial arts",
    "description": "provides students an opportunity to learn self-defense or combat that utilize physical skill and coordination without weapons."
    }
    ]
    }
    }

In this section, you will create an Atlas Search index for the fields in the embedded documents in the local_school_district.schools collection.

To create an Atlas Search index, you must have Project Data Access Admin or higher access to the project.

1
  1. If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

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

  3. If the Database Deployments page is not already displayed, click Database in the sidebar.

2
  1. Click your cluster's name.

  2. Click the Atlas Search tab.

3
4
  • For a guided experience, select Visual Editor.

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

5
  1. In the Index Name field, enter embedded-documents-tutorial.

    Note

    If you name your index default, you don't need to specify an index parameter when using the $search pipeline stage. Otherwise, you must specify the index name using the index parameter.

  2. In the Database and Collection section, find the local_school_district database, and select the schools collection.

6

To learn more about the index definition, see About the Atlas Search Index.

7

Atlas displays a modal window to let you know your index is building.

8

The index should take about one minute to build. While it is building, the Status column reads Build in Progress. When it is finished building, the Status column reads Active.

You can run queries against the embedded document fields. This tutorial uses embeddedDocument and text operators inside the compound operator in the queries.

In this section, you will connect to your Atlas cluster and run the sample queries using the operators against the fields in the schools collection.


Use the Select your language drop-down menu on this page to set the language of the examples in this section.


You can run $searchMeta queries against the embedded document fields. In this section, you will connect to your Atlas cluster and run a sample query using the $searchMeta stage and facet on an embedded document field.

← How to Run $unionWith with an Atlas Search $search Query