A wildcard search finds text that fits a pattern when some characters are unknown. Use `?` for one character and `*` for zero or more characters. It is useful for finding field values that are similar but not identical, especially when you have partial or uncertain information. section 11.5.1 section 11.2
In Atlas Search, place the `wildcard` operator inside the initial `$search` aggregation stage. Specify the search index, the field in `path`, and the pattern in `query`. The example searches inspection `business_name` values for `*L?CE*`; it sets `allowAnalyzedField: true` to search that analyzed field. section 11.5.1 section 11.5
db.inspections.aggregate([
{
$search: {
index: "MongoDB-in-Action",
wildcard: {
path: "business_name",
query: "*L?CE*", // Searches for phrases containing
➥ // "L?CE", where "?" represents any single
➥ // character and "*" represents zero
➥ // or more characters
allowAnalyzedField: true // Allows searching on an analyzed field
}
}
},
{
$set: {
score: { $meta: "searchScore" } // Adds the search
➥ // score to each document
}
},
{
$sort: {
score: -1 // Sorts by search score in descending order
}
},
{
$limit: 2 // Limits the number of results to 2
},
{
$unset: ["_id", "address", "date"] // Removes the '_id',
➥ // 'address', and 'date' fields
➥ // from the output documents
}
])
The pipeline adds each match’s search score, sorts by that score in descending order, keeps two results, and removes fields not needed in the output. section 11.5.1
Matching inspection names
The pattern returns business names containing `LICENSE`, including “UNNAMED HOT DOG VENDOR LICENSE NUMBER TA01158.” This is a pattern match, not a request for one exact business name. section 11.5.1
Choose wildcards when you know the shape of the text you want. For misspellings without a known pattern, fuzzy search is the alternative shown alongside wildcard search. At the indexed-term level, Lucene checks wildcard patterns against terms to find matches. section 11.0 section 11.2







