Below is the code snippet:
{$or: [
{titleLong: { $regex: regExp } },
{catalog: { $regex: regExp } },
{catalogNbr: { $regex: regExp } }
]}
I have a requirement to retrieve documents that match specific regex expressions in their fields. For instance, consider this document.
{course: {titleLong: "Introduction to analysis of algorithms"},
{catalog: "cs3333"},
{catalogNbr: "3333"}}
When a user enters "intro algo", "3333", or "cs3333", the document should be returned. I attempted using /(intro|algo)/gi
, but it fetches all documents containing either intro
or algo
. Furthermore, the global option g
does not seem effective. Another regex I tried is as follows:
(?=.*\bintro\b)(?=.*\balgo\b).+
However, this pattern only detects documents with exact matches like intro
and overlooks variations such as introduction
.