This tutorial demonstrates how to index and run MongoDB Search queries against fields that are inside an array of documents, even when the array of documents is nested itself. To enable queries against these fields, you index the document fields as the embeddedDocuments type.
The tutorial on this page demonstrates queries against fields in the following types of arrays:
Array of documents.
Array of documents inside a document.
Array of documents inside an array of documents.
Before you begin, ensure that your cluster meets the requirements described in the Prerequisites.
To demonstrate how to run queries against embedded documents, this tutorial walks you through the following steps:
Create a sample collection named
schoolswith embedded documents in your cluster.Set up a MongoDB Search index with embeddedDocuments fields configured at the following paths:
teachersfieldteachers.classesfieldclubs.sportsfield
Run
$searchqueries that search the embedded documents in theschoolscollection using the compound with the embeddedDocument and text.Run a
$searchMetaquery against an embedded document field to get a count.
About the Collection
The schools sample collection contains three
documents. Each document contains the name
and mascot of the school, the``first`` and last
name of each teacher, the classes that they teach including the
subject and grade, and the various clubs available.
About the Index
The index for this collection specifies the following documents in arrays:
Documents in the arrays at the
teachersandteachers.classespaths are indexed as embeddedDocuments, and the fields inside the documents are dynamically indexed.Documents in the arrays at the
teacherspath are also indexed as the document type to support highlighting, and the fields inside the documents are dynamically indexed.Document in the
clubsfield is indexed as the document type with dynamic mappings enabled and the arrays of documents in theclubs.sportsfield are indexed as theembeddedDocumentstype with dynamic mappings enabled.
About the Queries
The sample queries search the embedded documents in the schools
collection. The queries use the following pipeline stages:
$searchto search the collection.$projectto include and exclude fields from the collection, and add a field namedscorein the results. For queries that enable highlighting, the$projectstage also adds a new field calledhighlights, which contains the highlighting information.
The tutorials demonstrate the following queries:
This query demonstrates a search against a field inside an array of documents.
It searches at the teachers path for teachers with the first name
John and specifies a preference for teachers with the last name
Smith. It also enables highlighting on the
last name field.
This query demonstrates a search against a field inside an array of documents that is nested inside a document.
It searches for schools that have sports clubs that offer students an
opportunity to play either dodgeball or frisbee at the
clubs.sports path.
This query demonstrates a compound query that searches fields inside the following arrays:
Array of documents.
Array of documents nested inside an array of documents.
It searches for schools that have a teacher teaching 12th grade
science class at the teachers.classes path, preferring schools
with teachers with last name Smith who teach that class. It also
enables highlighting on the subject field
inside the classes array of the documents which is nested inside the
teachers array of documents.
Note
The tutorial for your cluster
also includes a $searchMeta query to get a
count of the number of schools that offer classes in each grade.
Run Queries Against Embedded Documents
This tutorial demonstrates sample MongoDB Search queries against fields in embedded documents. To learn more about these queries, see About this Tutorial.