Definition
wildcardThe
wildcardoperator enables queries which use special characters in the search string that can match any character.CharacterDescription?Matches any single character.
*Matches 0 or more characters.
\Escape character.
wildcardis a term-level operator, meaning that thequeryfield is not analyzed. Term-level operators work well with the Keyword Analyzer, because thequeryfield is treated as a single term, with special characters included. For an example of querying against an analyzedqueryfield, see the analyzed field example.
Syntax
wildcard has the following syntax:
{ $search: { "index": <index name>, // optional, defaults to "default" "wildcard": { "query": "<search-string>", "path": "<field-to-search>", "allowAnalyzedField": <boolean>, "score": <options> } } }
Options
wildcard uses the following terms to construct a query:
Field | Type | Description | Necessity | Default |
|---|---|---|---|---|
| string or array of strings | String or strings to search for. | yes | |
| string or array of strings | Indexed field or fields to search. You can also specify a wildcard path to search. | yes | |
| boolean | Must be set to | no |
|
| object | Modify the score assigned to matching search term results. Options are:
For information on using | no |
Behavior
wildcard is a term-level operator, meaning that the query field is not analyzed. It is possible to use the wildcard operator to perform searches on an analyzed field during indexing by setting the allowAnalyzedField option to true. If you use wildcard with allowAnalyzedField: true, MongoDB Search applies character filters and token filters based on the specified analyzer or custom analyzer for the field. MongoDB Search skips the tokenization and the output is always a single token.
The following examples show how the wildcard operator behaves when performing a search on analyzed fields:
Example
Standard Analyzer
Suppose that a field foo bar baz is indexed with the standard analyzer. MongoDB Search analyzes and indexes the field as foo, bar and baz. Searching for foo bar* on this field finds nothing, because the wildcard operator treats foo bar* as a single search term with a wildcard at the end. In other words, MongoDB Search searches the field for any term that begins with foo bar but finds nothing, because no term exists.
Example
Keyword Analyzer
Searching for *Star Trek* on a field indexed with the keyword analyzer finds all documents in which the field contains the string Star Trek in any context. Searching for *Star Trek* on a field indexed with the standard analyzer finds nothing, because there is a space between Star and Trek, and the index contains no spaces.
Escape Character Behavior
When using the escape character in mongosh or with a driver, you must use a double backslash before the character to be escaped.
Example
To create a wildcard expression which searches for any string containing a literal asterisk in an aggregation pipeline, use the following expression:
"*\\**"
The first and last asterisks act as wildcards which match any characters, and the \\* matches a literal asterisk.
Note
Use the following expression to escape a literal backslash:
"*\\\*"
Index Example
The following index definition indexes the title field in the movies collection with the keyword analyzer:
1 { 2 "mappings": { 3 "fields": { 4 "title": { 5 "analyzer": "lucene.keyword", 6 "type": "string" 7 } 8 } 9 } 10 }
Examples
The following examples use the movies collection in the sample_mflix database with a custom index definition that uses the keyword analyzer. If you have the sample dataset on your cluster, you can create a MongoDB Search index on the movies collection and run the example queries on your cluster.
Query Examples
The following example searches all title fields for movie titles that begin with Green D, followed by any number of other characters.
The following example searches all title fields for movie titles that begin with the string Wom?n (where ? may be any single character), followed by a space and then any number of additional characters.
The following example searches using the escape character for documents in which the title field ends with a question mark.
The * character in the query field matches any characters, and the \\? string matches a literal question mark.