skip to content
Alvin Lucillo

Unanchored regex

/ 1 min read

An unanchored regex pattern (1) and a pattern with wrapper of repeaters (2) have the same result. They basically behave like a “contains” function of a string. The first one matches documents with that word in the field because it has no anchors ^ and $. The second one matches documents with anything before and after the word.

  1. {first_name: {$regex: "ree"}}
  2. {first_name: {$regex: ".*ree.*"}}

It’s important to note that the second one can add backtracking effort. Basically, the wrapper causes redundant work.