Case Insensitive Search with Regex

I am trying to do a case-insensitive search using Regex - the documentation gives examples with literal strings but I am trying this using a variable instead. This search works but only with matching case - how can I make it case insensitive?

exports=function (payload) {
  
  const query={"dt_name" : {'$regex':"^"+payload.query.testparam.toString()+"$"}};
  const mongodb=context.services.get("mongodb-atlas");
  const mycollection = mongodb.db("dt3_api").collection("LCP Production");
 
return mycollection.find(query).limit(10).toArray();
};

Hello! @Richard_Kaplan .

By adding the i option in the regex pattern to perform a case-insensitive match for documents:

e.g

db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})

More info: https://docs.atlas.mongodb.com/schema-suggestions/case-insensitive-regex/

Hope this helps :slight_smile:

@HomAskIe

Thank you - that works if I literally hard code a string

What is the syntax if the string is in a variable - specifically a parameter from the URL i.e. payload.query.testparam

I tried this and various similar configurations:

return mycollection.find ({ "dt_name": { '$regex' : new RegExp(param1, $options:'i') } });

But I get:

“error”: “(BadValue) $regex has to be a string”

Ah I see. $regex expects the pattern to be a string or an expression. If you’d like to use RegExp then you need to convert it to string.

You code might become:

return mycollection.find ({"dt_name": { '$regex' : new RegExp(param1).toString(), $options: 'i' } });

You can include insensitive within RegExp too.

then it might become

return mycollection.find ({"dt_name": { '$regex' : new RegExp(param1, "i").toString()} });

notice there is an “i” right after param1.

Hope this helps. Thank you :slight_smile:

Much appreciated

I get no error now, but the result of the find is always blank with either syntax. Even if I replace param1 with the desired parameter in quotes the find is always blank.

However this more simplified version does work - but only as a case-sensitive search:

return mycollection.find ({"dt_name":param1});

How do I get it to work not as a case-sensitive search?

hum… interesting. Could you share with me some sample of your data?

See private message… thanks

I’m not 100% sure why that’s not working for you because It works for me.

What is the value of param1 look like?

Try to make a loop to log each doc after before return see what is the output.
Something like:

data = mycollection.find ({"dt_name": { '$regex' : new RegExp("Kaplan", "i").toString()} });
// or
// data = mycollection.find ({"dt_name": { '$regex' : 'Kaplan', "$options": "i" } });
for (const d of data) {
    console.log(d);
}
return data

You can see param1 as testparam in Postman - It is currently set as Kaplan and I am hoping for it to work with both Kaplan and kaplan

Your proposed code as a Realm function yields this:

Realm looks like this:

I see. Could you let me know which mongo driver you are using?

Try this to see it work

const param1 = "kaplan"  // You might change this to be dynamic from query string
const docs = mycollection.find({
    "dt_name": {
        "$regex": param1, "$options": "i"
    }
});
const data = await docs.toArray();
console.log("Doc Len", data.length);
return data;

MongoDB 4.4.8

That code in Realm gives me this error shown on top:

Ah that’s on Realm Function Editor! My bad. Even I’m new to that tool as well ha ha

All right, I just set up Realm and figure it out.
Try this simple way. It works for me on Ream as well.

const docs = mycollection.find({
    "dt_name": {
        "$regex": param1,
        "$options": "i"
    }
});
return data;

Have you tried this way? :slight_smile:

I think you meant const data instead of const docs

Yes it works that way! Huge thanks!!!

Can you explain why the other methods did not work?

Hi! I’ve been trying to make it works with RegExp on Realm but have not solved it yet.
It seems like the the slashes when convert a RegExp object to string is the problem.
e.g
This won’t work

let pattern = new RegExp("abc").toString()
console.log(pattern);
"/abc/"
1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.