Case insensitive MongoDB query from Go

六眼飞鱼酱① 提交于 2019-12-23 02:21:41

问题


I have this json file:

[{
    "name": "chetan",
    "age": 23,
    "hobby": ["cricket", "football"]
}, {
    "name": "raj",
    "age": 24,
    "hobby": ["cricket", "golf"]
}]

I use this Go code to search the data:

id := "ket" 
regex := bson.M{"$regex": bson.RegEx{Pattern: id}} 
err = c.Find(bson.M{"hobby": regex}).All(&result)

It finds if searched by the same string like "cricket" but if I search string like this "Cricket", it does not find it.


回答1:


Add Options: "i" to your RegEx.

bson.M{"$regex": bson.RegEx{Pattern: id, Options: "i"}}


来源:https://stackoverflow.com/questions/37340539/case-insensitive-mongodb-query-from-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!