1

I am using Mongo's Primitive package to get a bson value based on what was submitted. This is what I am currently doing

school = "Havard"
value = primitive.Regex{Pattern: school, Options: ""} 

This will only match bson values that are Havard, how do I make this regex case insensitive and make it match for example, hava

In all, if I use hava for a search, I should also get Havard

2 Answers 2

2

The expression primitive.Regex{Pattern: school} matches substrings too, but it's not case insensitive. Use the "i" option to make it case insensitive:

value = primitive.Regex{Pattern: school, Options: "i"}

Also note that if the value of school contains special regexp characters, that might give you unexpected results or errors. So best is to quote it with e.g. using regexp.QuoteMeta():

value = primitive.Regex{Pattern: regexp.QuoteMeta(school), Options: "i"}
0

For GO users the filter looks like this:

filter := bson.D{{"column_name", primitive.Regex{Pattern: school, Options: "i"}}}
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Apr 6, 2022 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.