Para agentes de IA: hay un índice de documentación disponible en https://www.mongodb.com/es/docs/llms.txt; las versiones en Markdown de todas las páginas están disponibles agregando .md a cualquier ruta URL.
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Docs Menu

Cómo ejecutar Autocompletar y coincidencia parcial de los queries de búsqueda de MongoDB

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:

  1. Set up a MongoDB Search index on the plot field in the sample_mflix.movies collection.

  2. Run MongoDB Search query for a partial string against the plot field in the sample_mflix.movies collection 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:

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.

1
  1. If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it is not already displayed, select your desired project from the Projects menu in the navigation bar.

  3. Click your cluster name.

  4. Click the Search tab.

2
3
4
5
6

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"
}
}
}
}
7
8
9

A modal window appears to let you know your index is building. Click Close.

10

The index should take about one minute to build. While it is building, the Status column reads Build in Progress. When it is finished building, the Status column reads Active.

1

You can go the MongoDB Search page from the Search & Vector Search option, or the Data Explorer.

  1. If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

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

  1. If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

  3. In the sidebar, click Data Explorer under the Database heading.

  4. Expand the database and select the collection.

  5. Click the Indexes tab for the collection.

  6. Click the Search and Vector Search link in the banner.

    The Search & Vector Search page displays.

2
3
4

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"
}
}
5

MongoDB Search returns the search results for documents in the movies collection.

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.

En esta página