Golang mgo 模糊查询的使用
发布时间:2020-12-16 09:25:05 所属栏目:大数据 来源:网络整理
导读:在日常使用的Mongodb中,有一项功能叫做模糊查询(使用正则匹配),例如: db.article.find({"title": {$regex: /a/,$options: "im"}}) 这是我们常用Mongodb的命令行使用的方式,但是在mgo中做出类似的方式视乎是行不通的: query := bson.M{"title": bson.M{
在日常使用的Mongodb中,有一项功能叫做模糊查询(使用正则匹配),例如: db.article.find({"title": {$regex: /a/,$options: "im"}}) 这是我们常用Mongodb的命令行使用的方式,但是在mgo中做出类似的方式视乎是行不通的: query := bson.M{"title": bson.M{"$regex": "/a/","$options": "im"}} 大家用这个方式去查询,能查询到算我输!
query := bson.M{"title": bson.M{"$regex": "a","$options": "im"}}
// RegEx represents a regular expression. The Options field may contain // individual characters defining the way in which the pattern should be // applied,and must be sorted. Valid options as of this writing are 'i' for // case insensitive matching,'m' for multi-line matching,'x' for verbose // mode,'l' to make w,W,and similar be locale-dependent,'s' for dot-all // mode (a '.' matches everything),and 'u' to make w,and similar match // unicode. The value of the Options parameter is not verified before being // marshaled into the BSON format. type RegEx struct { Pattern string Options string } 那么最终我们的代码为: query := bson.M{"title": bson.M{"$regex": bson. RegEx:{Pattern:"/a/",Options: "im"}}} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |