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
schools
with embedded documents in your cluster.Set up a MongoDB Search index with embeddedDocuments fields configured at the following paths:
teachers
fieldteachers.classes
fieldclubs.sports
field
Run
$search
queries that search the embedded documents in theschools
collection using the compound with the embeddedDocument and text.Run a
$searchMeta
query 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
teachers
andteachers.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 theclubs.sports
field are indexed as theembeddedDocuments
type 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:
$search
to search the collection.$project
to include and exclude fields from the collection, and add a field namedscore
in the results. For queries that enable highlighting, the$project
stage 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.