BSON是什么?
BSON就是二进制编码的JSON序列化数据。
官网上提到的三个特点有:
- 更轻量
- 可转换(序列化和反序列化)
- 更高效,因为是二进制的
BSON在mongdo-driver中的应用
根据上面所说的BSON的特点,MongoDB是用BSON作为主要的数据格式
go.mongodb.org/mongo-driver/bson 的使用
bson struct
在进行mongodb操作时,经常需要一些基本的bson结构体。有四种struct可以定义bson的数据结构:bson.D{}、bson.E{}、bson.M{}、bson.A{}
先贴上bson.go的源码,其实说得很清楚了
// 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 elemen

本文介绍了BSON这种二进制编码的JSON序列化数据格式,强调其轻量、可转换和高效特性。在MongoDB驱动中,BSON作为主要数据格式。文章详细阐述了`go.mongodb.org/mongo-driver/bson`包的使用,包括`bson struct`和`bson/primitive`子包。`bson struct`用于定义MongoDB操作所需的结构体,而`primitive`包提供了诸如ObjectID和Regex等常用数据结构及操作方法。
最低0.47元/天 解锁文章
3662

被折叠的 条评论
为什么被折叠?



