使用mongo的原始包进行部分匹配 [英] Partial matches using mongo's primitive package
问题描述
我正在使用Mongo的 Primitive
包来根据提交的内容获取bson值.这是我目前正在做的
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: ""}
这只会匹配 Havard
的bson值,如何使此正则表达式不区分大小写并使其匹配,例如 hava
This will only match bson values that are Havard
, how do I make this regex case insensitive and make it match for example, hava
总而言之,如果我使用 hava
进行搜索,我也应该获得 Havard
In all, if I use hava
for a search, I should also get Havard
推荐答案
表达式 primitive.Regex {Pattern:school}
也会匹配子字符串,但不区分大小写.使用"i"
选项使其不区分大小写:
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"}
还请注意,如果 school
的值包含特殊的正则表达式字符,则可能会给您带来意外的结果或错误.所以最好用例如使用 regexp.QuoteMeta()
:
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"}
这篇关于使用mongo的原始包进行部分匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!