liveBook · Manning

Wildcard

As explained in MongoDB 8.0 in Action, Third Edition

A wildcard expression specifies a text-search pattern containing placeholder characters that match variable text.

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

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines both placeholders and walks through an Atlas Search pipeline; briefly notes indexed-term matching without detailing its internals.
Examples
Uses a `business_name` query for `*L?CE*` and shows matching inspection records ordered by search score.
What it assumes
Assumes a searchable Atlas collection and named index while showing how to specify the field and pattern in the query.
What you can do after
Apply it via Atlas Search to retrieve pattern-matching records and shape the returned results with aggregation stages.
How it connects
Connects wildcard search to Atlas Search, aggregation pipelines, Lucene indexed terms, and fuzzy search.

Also explained in

Compare how 8 books explain wildcard →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.