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格式了

原文地址:https://www.cnblogs.com/leescre/p/12625240.html