Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs Menu

How to Run MongoDB Search Queries Against Fields in Embedded Documents

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.

Expand this section to learn more about the sample collection, index, and queries used in this tutorial.

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

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

  2. Set up a MongoDB 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 with the embeddedDocument and text.

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

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.

The index for this collection specifies the following documents in arrays:

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

This tutorial demonstrates sample MongoDB Search queries against fields in embedded documents. To learn more about these queries, see About this Tutorial.