• golang mongo-driver filter 构建--bson和golang基础类型


    go.mongodb.org/mongo-driver 是mongo的golang官方包

    通过例子我知道连接是这样的
    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参数, 等同于命令行里的

    db.usertag.find({$or :[{'rid': 1},{'aid': ''},{'uqid': ''}]});
    

      但是在代码的实现例filter并不能写成这样

    filter := `{$or :[{'rid': 1},{'aid': ''},{'uqid': ''}]}`
    

      这样是不识别的, 我们需要用到bson里提供的几种结构体

    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就构造成这样的

    filter := bson.M{"$or": bson.A{bson.M{"rid":1},bson.M{"aid":"a"}}}
    

     我们再去看M,A底层数据结构是什

    // 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也可以写成这样

    filter := map[string]interface{}{"$or":[]interface{}{map[string]interface{}{"rid":1}}}
    

      

    所以我们就可以对照mongo命令文档转成golang代码里的filter了

    原因在与mongo底层的通信都是bson(二进制json), 没办法识别字符串, mongo命令行识别是因为client基于json字符串为基础, 去转换成bson格式了

  • 相关阅读:
    Switch
    java 函数 运算符
    java 基本类型
    更新时电话查重
    微信公众平台发送模板消息时连发三遍的最简单解决办法
    Yii2.0 发送邮件时中文附件乱码的问题
    Yii2.0 发送文件
    Yii2.0 请求
    Yii2.0随笔 路由
    yii2.0 在save保存之前的操作(放在模型model文件内)
  • 原文地址:https://www.cnblogs.com/leescre/p/12625240.html
Copyright © 2020-2023  润新知