How to Run Atlas Search Queries Against Objects in Arrays
On this page
This tutorial describes how to index and run queries against fields in documents that are inside an array. This tutorial takes you through the following steps:
Create a sample collection named
schools
with embedded documents in your Atlas cluster.Set up an Atlas Search index with embeddedDocuments fields configured at the
teachers
andteachers.classes
paths.Run Atlas Search queries that search the embedded documents in the the
schools
collection using the compound operator with the embeddedDocument and text operators.
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.
Create a Sample Collection and Load the Data
Each document in the sample collection named schools
contains the
name
and mascot
of the school, school teacher's first
and
last
name, the classes
that each teacher teaches including the
subject
name and grade
level.
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.
The steps in this section walk you through creating a new database
and collection, and loading the sample data into your collection.
Load the following documents into the collection.
Select the
schools
collection if it's not selected.Click Insert Document for each of the sample documents to add to the collection.
Click the JSON view ({}) to replace the default document.
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" }] }] } { "_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" }] }] } { "_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" }] }] }
Create an Atlas Search Index
In this section, you will create an Atlas Search index for the fields in the
embedded documents in the local_school_district.schools
collection.
Navigate to the Atlas Search page for your project.
If it isn't already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it isn't already displayed, select your desired project from the Projects menu in the navigation bar.
Click your cluster's name.
Click the Search tab.
Enter the Index Name, and set the Database and Collection.
In the Index Name field, enter
default
.Note
If you name your index
default
, you don't need to specify anindex
parameter when using the $search pipeline stage. Otherwise, you must specify the index name using theindex
parameter.In the Database and Collection section, find the
local_school_district
database, and select theschools
collection.
Specify an index configuration that indexes embedded documents.
Replace the default index definition with the following index definition and click Next.
The following index definition specifies that documents in the arrays at the
teachers
andteachers.classes
paths must be indexed as embeddedDocuments, and the fields inside the documents must be dynamically indexed.1 { 2 "mappings": { 3 "fields": { 4 "teachers": { 5 "type": "embeddedDocuments", 6 "dynamic": true, 7 "fields": { 8 "classes": { 9 "type": "embeddedDocuments", 10 "dynamic": true 11 } 12 } 13 } 14 } 15 } 16 } Click Create Search Index.
Run Atlas Search Queries Against Embedded Document Fields
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.