golang mongo-driver filter 构建--bson和golang基础类型
go.mongodb.org/mongo-driver 是mongo的golang官方包
通过例子我知道连接是这样的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | clientOptions := options.Client().ApplyURI( "mongodb://tag:123456@127.0.0.1:27017/tag" ) client, err := mongo.NewClient(clientOptions) if err != nil { fmt.Println(err) } ctx, _ := context.WithTimeout(context.Background(), 2*time.Second) err = client.Connect(ctx) err = client.Ping(ctx, readpref.Primary()) //tag := UserTag{ // Rid: 1, // Aid: "aid", // Uqid: "", // Values: map[string]string{"1": "a", "2": "b"}, //} collection := client.Database( "tag" ).Collection( "usertag" ) ctx, _ = context.WithTimeout(context.Background(), 5*time.Second) t := UserTag{} //db,usertag.find({$or :[{'rid': 1},{'aid': ''},{'uqid': ''}]}); //err = collection.FindOne(ctx,bson.M{"$or": bson.A{bson.M{"rid":1},bson.M{"aid":"a"}}}).Decode(&t) err = collection.FindOne(ctx, map [string] interface {}{ "$or" :[] interface {}{ map [string] interface {}{ "rid" :1}}}).Decode(&t) if err != nil { fmt.Println(err) return } fmt.Println(t) |
所有的find方法都有一个filer参数, 等同于命令行里的
1 | db.usertag.find({$or :[{ 'rid' : 1},{ 'aid' : '' },{ 'uqid' : '' }]}); |
但是在代码的实现例filter并不能写成这样
1 | filter := `{$or :[{ 'rid' : 1},{ 'aid' : '' },{ 'uqid' : '' }]}` |
这样是不识别的, 我们需要用到bson里提供的几种结构体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | type D = primitive.D // E represents a BSON element for a D. It is usually used inside a D. type E = primitive.E // M is an unordered representation of a BSON document. This type should be used when the order of the elements does not // matter. This type is handled as a regular map[string]interface{} when encoding and decoding. Elements will be // serialized in an undefined, random order. If the order of the elements matters, a D should be used instead. // // Example usage: // // bson.M{"foo": "bar", "hello": "world", "pi": 3.14159} type M = primitive.M // An A is an ordered representation of a BSON array. // // Example usage: // // bson.A{"bar", "world", 3.14159, bson.D{{"qux", 12345}}} type A = primitive.A |
其中M是map A是列表
所以我们的filter就构造成这样的
1 | filter := bson.M{ "$or" : bson.A{bson.M{ "rid" :1},bson.M{ "aid" : "a" }}} |
我们再去看M,A底层数据结构是什
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // M is an unordered representation of a BSON document. This type should be used when the order of the elements does not // matter. This type is handled as a regular map[string]interface{} when encoding and decoding. Elements will be // serialized in an undefined, random order. If the order of the elements matters, a D should be used instead. // // Example usage: // // bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}. type M map [string] interface {} // An A is an ordered representation of a BSON array. // // Example usage: // // bson.A{"bar", "world", 3.14159, bson.D{{"qux", 12345}}} type A [] interface {} |
所以我们的filter也可以写成这样
1 | filter := map [string] interface {}{ "$or" :[] interface {}{ map [string] interface {}{ "rid" :1}}} |
所以我们就可以对照mongo命令文档转成golang代码里的filter了
原因在与mongo底层的通信都是bson(二进制json), 没办法识别字符串, mongo命令行识别是因为client基于json字符串为基础, 去转换成bson格式了
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!