网站怎么自适应屏幕大小,沈阳网站排名工具,福州网站建设营销方案,和建设银行类似的网站1.Bson的类型
bson对象是键值对对象#xff0c;bson是JSON的二进制格式。go操作mongoDB数据库的时候经常使用bson键值对作为筛选条件。
D家族#xff0c;可以简单的构建BSON对象。
D#xff1a;一个BSON文档#xff0c;这种类型应该在顺序重要的情况下使用。 每一对键值…1.Bson的类型
bson对象是键值对对象bson是JSON的二进制格式。go操作mongoDB数据库的时候经常使用bson键值对作为筛选条件。
D家族可以简单的构建BSON对象。
D一个BSON文档这种类型应该在顺序重要的情况下使用。 每一对键值对都包含一个大括号bson.D{{keyvalue},{keyvalue}}中间用逗号连接keyvalue。
M一个无序的map它和D是一样的只是它不保持顺序。 每一对键值对不使用大括号bson.M{key:value}中间用 冒号key:value 进行连接。
A一个BSON数组当使用“$and”,“$or”等要使用数组。
ED里面的一个元素。
package bson // import go.mongodb.org/mongo-driver/bsonimport (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
1bson.D
bson.D是一种有序的键值对DDocument格式代表了一个BSON文档。D包含了一个E切片
type D []E
每一对键值对都需要用大括号来连接bson.D{{key,value},{key,value}...}键和值之间使用逗号连接比如
bson.D{{foo, bar}, {hello, world}, {pi, 3.14159}}
2bson.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},
}
3bson.M
bson.M是无序的键值对键值对之间使用冒号连接比如
bson.M{foo: bar, hello: world, pi: 3.14159}
4bson.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过滤条件这个用的多一些。