This tutorial describes how to create an index on the sample_mflix.movies collection and run partial string queries against the plot field using the Atlas UI.
To return matches for partial string queries, you can use one of the following operators:
The autocomplete operator, which allows you to search the specified fields for a word or phrase that contains the sequence of characters that you specify with your query.
The phrase operator, which allows you to search the specified fields for documents that contain the terms in your query string at the distance you specify between the terms.
The regex operator, which allows you to search the specified fields for strings using regular expression.
The wildcard operator, which allows you to search the specified fields using special characters in your query to match any character.
You can also use the text operator with a custom analyzer for more fine-grained control over partial matching. To learn more, see Custom Analyzers.
This tutorial takes you through the following steps:
Set up a MongoDB Search index on the
plotfield in thesample_mflix.moviescollection.Run MongoDB Search query for a partial string against the
plotfield in thesample_mflix.moviescollection using autocomplete, phrase, regex, and wildcard operators.
Before you begin, ensure that your cluster meets the following requirements.
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 Adminor 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.
Create the MongoDB Search Index
Navigate to the MongoDB Search page for your project.
If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it is not already displayed, select your desired project from the Projects menu in the navigation bar.
Click your cluster name.
Click the Search tab.
Replace the default definition with the following index definition.
The following index definition creates an autocomplete index on the plot field.
{ "mappings": { "fields": { "plot": { "type": "autocomplete" } } } }
The following index definition creates a string index on the plot field.
{ "mappings": { "fields": { "plot": { "type": "string" } } } }
Run a Case-Sensitive Partial Match Query
In Atlas, go to the Search & Vector Search page for your cluster.
You can go the MongoDB Search page from the Search & Vector Search option, or the Data Explorer.
If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.
If it's not already displayed, select your project from the Projects menu in the navigation bar.
In the sidebar, click Search & Vector Search under the Database heading.
If you have no clusters, click Create cluster to create one. To learn more, see Create a Cluster.
If your project has multiple clusters, select the cluster you want to use from the Select cluster dropdown, then click Go to Search.
The Search & Vector Search page displays.
If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.
If it's not already displayed, select your project from the Projects menu in the navigation bar.
In the sidebar, click Data Explorer under the Database heading.
Expand the database and select the collection.
Click the Indexes tab for the collection.
Click the Search and Vector Search link in the banner.
The Search & Vector Search page displays.
Copy and paste the following query into the Query editor.
The following query searches for documents where the plot field contains words that begin with haw.
{ "autocomplete": { "query": "haw", "path": "plot" } }
The following query searches for documents where the plot field contains the phrase new york with a maximum distance of 5 between terms.
{ "phrase": { "query": "new york", "path": "plot", "slop": 5 } }
The following query searches for documents where the plot field contains words that match the regular expression pattern.
{ "regex": { "query": "(?i)new.*york", "path": "plot" } }
The following query searches for documents where the plot field contains words that match the wildcard pattern.
{ "wildcard": { "query": "new*york", "path": "plot" } }
Continuar Aprendiendo
Para aprender más sobre cómo implementar coincidencias parciales, consulta Búsqueda de texto en MongoDB: coincidencia de patrones de subcadenas que incluye Regex y comodín. En su lugar, usa Búsqueda.