来自Go的不区分大小写的MongoDB查询 [英] Case insensitive MongoDB query from Go

查看:171
本文介绍了来自Go的不区分大小写的MongoDB查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  [{
name:chetan,
年龄:23,
爱好:[cricket,football]
},{
name:raj,
age :24,
hobby:[cricket,golf]
}]

我使用Go代码搜索数据:

  id:=ket
regex := {bson.M $正则表达式:bson.RegEx {图样:ID}}
ERR = c.Find(bson.M { 爱好:正则表达式})。所有(安培;结果)

它查找是否像cricket一样搜索相同的字符串,但如果我像这样搜索字符串Cricket

解决方案

添加选项:我您的RegEx。

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


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.

解决方案

Add Options: "i" to your RegEx.

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

这篇关于来自Go的不区分大小写的MongoDB查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆