本文主要是介绍go中bson的基本操作,bson.M,bson.D,bson.A,bson.E,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.Bson的类型
bson对象是键值对对象,bson是JSON的二进制格式。go操作mongoDB数据库的时候经常使用bson键值对作为筛选条件。
D家族,可以简单的构建BSON对象。
D:一个BSON文档,这种类型应该在顺序重要的情况下使用。 每一对键值对都包含一个大括号,bson.D{{key,value},{key,value}},中间用逗号连接key,value。
M:一个无序的map,它和D是一样的,只是它不保持顺序。 每一对键值对不使用大括号,bson.M{key:value},中间用 冒号key:value 进行连接。
A:一个BSON数组,当使用“$and”,“$or”等要使用数组。
E:D里面的一个元素。
package bson // import "go.mongodb.org/mongo-driver/bson"import ("go.mongodb.org/mongo-driver/bson/primitive"
)
// D is an ordered representation of a BSON document. This type should be used when the order of the elements matters,
// such as MongoDB command documents. If the order of the elements does not matter, an M should be used instead.// Example usage:
// bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
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
// 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
(1)bson.D
bson.D是一种有序的键值对,D(Document)格式代表了一个BSON文档。D包含了一个E切片,
type D []E
每一对键值对都需要用大括号来连接,bson.D{{key,value},{key,value}...},键和值之间使用逗号连接,比如:
bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
(2)bson.E
bson.D有序键值对的其中一个元素。一个bson.D由多个bson.E构成。
// E represents a BSON element for a D. It is usually used inside a D.
type E struct {Key stringValue interface{}
}bson.D{bson.E{Key: "name", Value: "zhangsan"},bson.E{Key: "age", Value: 19},
}
(3)bson.M
bson.M是无序的键值对,键值对之间使用冒号连接,比如:
bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}
(4)bson.A
bson.A是一个bson数组,当使用“$and”,等等,会使用数组。比如:
a := bson.A{"bar","cars",bson.M{"name": "zhangsan","age": 18,},bson.E{Key: "address",Value: "China",},bson.D{{"cinder", "kingsoft"},{"glance", "kingsoft"},},}
bson.D和bson.M区别
①bson.D是有序的,bson.M是无序的。
②bson.D的键值对需要大括号括起来,bson.M不需要
③bson.M键值对使用:链接,bson.D键值对使用逗号,连接
2.举例
func bsonTestM() {docs := bson.M{"name": "Alice","age": 18,// addr是一个无序的键值对"addr": bson.M{"city": "beijing","country": "China",},}// 序列化字节数组 func Marshal(val interface{}) ([]byte, error) {data, err := bson.Marshal(docs)if err != nil {panic(err)}fmt.Println("输出序列化的data内容:", data)var result bson.M// 方便将字节[]byte映射到结果集中。err = bson.Unmarshal(data, &result)if err != nil {panic(err)}fmt.Println("输出反序列化后的内容:", result)fmt.Printf("bson.M类型为%T", bson.M{})
}// 有序的键值对对象
func bsonTestD() {docs := bson.D{{"name", "zhangsan"},{"age", 18},}bytes, err := bson.Marshal(docs)if err != nil {panic(err)}var result bson.D// 为什么使用&result// 因为Unmarshal函数需要修改目标对象的值,而不仅仅是复制数据。通过传递指针,函数可以直接在目标对象的内存地址上进行操作,// 从而避免了对整个对象进行复制,节省了内存和处理时间。//另外,通过传递指针,可以确保Unmarshal函数可以正确地更新目标对象的值,而不是创建一个新的对象。/*总之,&result,其实就是为了可以直接在目标对象的内存地址进行操作,避免对整个对象的复制,节省了内存和处理时间。*/err = bson.Unmarshal(bytes, &result)if err != nil {panic(err)}fmt.Println(result)
}func testBsonA() {a := bson.A{"bar","cars",bson.M{"name": "zhangsan","age": 18,},bson.E{Key: "address",Value: "China",},bson.D{{"cinder", "cinder"},{"glance", "demo"},},}fmt.Println(a)
}
其实用的比较多的是在go操作mongoDB的时候,因为mongoDB是键值对的数据库,bson文档可以作为filter过滤条件,这个用的多一些。
这篇关于go中bson的基本操作,bson.M,bson.D,bson.A,bson.E的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!