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 higher or a MongoDB self-managed cluster with MongoDB version 8.2 or higher.
The sample data loaded into your cluster.
Project Data Access Admin
or higher access to your project to create MongoDB Search indexes.Search Tester,
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
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.
Query Non-String Fields with String-Specific Operators
Certain MongoDB Search operators, like queryString and autocomplete, 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. 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.
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 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.
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
stage with$search
.To combine search results from multiple collections, use the
$unionWith
stage.To consolidate multiple collections so you can index and search them together, create a materialized view.
To get started, see How to Run MongoDB Search Queries Across Multiple Collections.