Design Search for Your Data Model

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

Design Search for Your Data Model

MongoDB Search provides several methods to help you optimize queries for different data models. Use the following tutorials to learn common design patterns so you can effectively query your data with MongoDB Search.

Prerequisites

To complete the tutorials, you must have:

  • An Atlas cluster with MongoDB version 6.0 or later or a MongoDB self-managed cluster with MongoDB version 8.2 or later.

  • The [sample data](https://www.mongodb.com/docs/atlas/import/load-sample-data.md#std-label-load-sample-data loaded into your cluster.

  • [Project Data Access Admin](https://www.mongodb.com/docs/atlas/reference/user-roles.md#mongodb-authrole-Project-Data-Access-Admin or higher access to your project to create MongoDB Search indexes.

  • Search Tester, [mongosh](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh, Compass, or a supported MongoDB Driver to run queries on your cluster.

Note:

You can run MongoDB Search queries by using any driver through the [$search](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search aggregation stage. These tutorials include examples for a selection of clients. Refer to the specific tutorial page for details.

You can also complete these tutorials with local deployments that you create with the Atlas CLI or with an on-prem deployment. To learn more, see Create a Local Atlas Deployment and [Self-Managed Deployments.](https://www.mongodb.com/docs/manual/self-managed-deployments.md#std-label-self-managed-deployments

Query Non-String Fields with String-Specific Operators

Certain MongoDB Search operators, like [queryString](https://www.mongodb.com/docs/search/query/operators-collectors/querystring.md#std-label-querystring-ref and [autocomplete](https://www.mongodb.com/docs/search/query/operators-collectors/autocomplete.md#std-label-autocomplete-ref, support only string fields. If you need to query non-string fields using these operators, you can convert the values of these fields to strings and store the converted fields in a [materialized view](https://www.mongodb.com/docs/manual/core/materialized-views.md#std-label-manual-materialized-views. A materialized view lets you use string-specific operators to query the converted fields and keep the original data intact in the source collection.

To get started, see [How to Search Non-Alphabetical Data as Strings.](https://www.mongodb.com/docs/search/tutorial/string-operators-tutorial.md#std-label-query-nonstring-tutorial

Query Fields Inside Embedded Documents

You can use MongoDB Search to query fields inside an array of documents, even when the array of documents is nested. After you index the document fields as the [embeddedDocuments](https://www.mongodb.com/docs/search/indexes/field-types/embedded-documents-type.md#std-label-bson-data-types-embedded-documents type, you can run queries against the fields inside these documents, including fields inside the following types of arrays:

  • Array of documents.

  • Array of documents inside a document.

  • Array of documents inside an array of documents.

To get started, see [How to Run MongoDB Search Queries Against Fields in Embedded Documents.](https://www.mongodb.com/docs/search/tutorial/embedded-documents-tutorial.md#std-label-embedded-documents-tutorial

Query Across Multiple Collections

When your search must span multiple collections, you can use the following methods to search across them:

  • To join collections and search across them, use the [$lookup](https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup.md#mongodb-pipeline-pipe.-lookup stage with [$search.](https://www.mongodb.com/docs/search/query/aggregation-stages/search.md#mongodb-pipeline-pipe.-search

  • To combine search results from multiple collections, use the [$unionWith](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unionWith.md#mongodb-pipeline-pipe.-unionWith stage.

  • To consolidate multiple collections so you can index and search them together, create a [materialized view.](https://www.mongodb.com/docs/manual/core/materialized-views.md#std-label-manual-materialized-views

To get started, see [How to Run MongoDB Search Queries Across Multiple Collections.](https://www.mongodb.com/docs/search/tutorial/cross-collection-tutorials.md#std-label-fts-cross-collection-tutorials