zoukankan      html  css  js  c++  java
  • 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格式了

  • 相关阅读:
    读写文件
    c++ 中的 -> :: : .
    CDH安装步骤
    MySQL安全配置向导mysql_secure_installation详解
    Linux下彻底卸载mysql详解
    安装mysql警告: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
    为Hadoop集群选择合适的硬件配置
    利用cm压缩包手动安装cm和cdh
    CM5.11与CDH5.11安装使用说明
    法的本体
  • 原文地址:https://www.cnblogs.com/leescre/p/12625240.html
Copyright © 2011-2022 走看看