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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM