{"success":true,"name":"use-mongodb-text-or-atlas-search-instead-of-unanchored-regex","description":"Building a search feature over MongoDB string fields","category":"ai-brimind","path":"ai-brimind/use-mongodb-text-or-atlas-search-instead-of-unanchored-regex/SKILL.md","file":"SKILL.md","content":"---\nname: use-mongodb-text-or-atlas-search-instead-of-unanchored-regex\ndescription: \"Building a search feature over MongoDB string fields\"\n---\n\n# Use MongoDB Text or Atlas Search Instead of Unanchored Regex\n\n## When to use\nProject uses MongoDB, and users need to search documents by keywords, names, or partial text.\n\n## Example request\nfind({ name: /smith/i }) on a big collection → an unanchored case-insensitive regex cannot use the index; full scan every keystroke. Prefix search 'starts with sm' → ^sm anchored regex DOES use a normal index; that case is fine. Real keyword search with relevance → $text with a text index, or Atlas Search for typo tolerance and ranking. Search-as-you-type autocomplete → Atlas Search autocomplete type exists for exactly this; hand-rolled regex is the reinvention. Each maps to 'search is a solved feature — pick the built-in that matches the search kind'.\n\n## Instructions\nAct as a developer who classifies the KIND of search before writing any search query against MongoDB. When text searching is requested: (1) name the case — exact match, prefix (starts-with), keyword/full-text, fuzzy/typo-tolerant, autocomplete — because each has a different correct tool, (2) map to the mechanism: exact match uses a normal index; prefix uses an anchored ^regex on an indexed field (index-friendly); keyword search uses a $text index with $meta relevance scoring; fuzzy, ranked, faceted, or autocomplete search uses Atlas Search if the project is on Atlas, (3) know the trap you are avoiding: an unanchored or case-insensitive regex ignores indexes and scans the whole collection, which feels fine on test data and collapses in production, (4) understand $text limits before choosing it (one text index per collection, language stemming behavior) so the choice is informed. If search requirements (typo tolerance? ranking? which fields?) are vague, ask — they decide the architecture. Only implement once the search kind is mapped to its mechanism, and explain why the search stays fast as the collection grows.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1146\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}