【转】BSON数据格式
原文:https://www.e-learn.cn/content/qita/1999197
-------------------------------------------------------------
BSON
https://baike.baidu.com/item/BSON
概念
BSON()是一种类json的一种二进制形式的存储格式,简称Binary JSON,它和JSON一样,支持内嵌的文档对象和数组对象,但是BSON有JSON没有的一些数据类型,如Date和BinData类型。BSON可以做为网络数据交换的一种存储形式,这个有点类似于Google的Protocol Buffer,但是BSON是一种schema-less的存储形式,它的优点是灵活性高,但它的缺点是空间利用率不是很理想,BSON有三个特点:轻量性、可遍历性、高效性{"hello":"world"} 这是一个BSON的例子,其中"hello"是key name,它一般是cstring类型,字节表示是cstring::= (byte*) "/x00" ,其中*表示零个或多个byte字节,/x00表示结束符;后面的"world"是value值,它的类型一般是string,double,array,binarydata等类型。使用情况
编辑MongoDB使用了BSON这种结构来存储数据和网络数据交换。把这种格式转化成一文档这个概念(Document),因为BSON是schema-free的,所以在MongoDB中所对应的文档也有这个特征,这里的一个Document也可以理解成关系数据库中的一条记录(Record),只是这里的Document的变化更丰富一些,如Document可以嵌套。MongoDB以BSON做为其存储结构的一种重要原因是其可遍历性。
官网
http://bsonspec.org/
BSON [bee · sahn], short for Binary JSON, is a binary-encoded serialization of JSON-like documents. Like JSON, BSON supports the embedding of documents and arrays within other documents and arrays. BSON also contains extensions that allow representation of data types that are not part of the JSON spec. For example, BSON has a Date type and a BinData type.
BSON can be compared to binary interchange formats, like Protocol Buffers. BSON is more "schema-less" than Protocol Buffers, which can give it an advantage in flexibility but also a slight disadvantage in space efficiency (BSON has overhead for field names within the serialized data).
BSON was designed to have the following three characteristics:
Lightweight
Keeping spatial overhead to a minimum is important for any data representation format, especially when used over the network.
Traversable
BSON is designed to be traversed easily. This is a vital property in its role as the primary data representation for MongoDB.
Efficient
Encoding data to BSON and decoding from BSON can be performed very quickly in most languages due to the use of C data types.
JSON比较
https://www.mongodb.com/json-and-bson
Binary JSON (BSON)
MongoDB represents JSON documents in binary-encoded format called BSON behind the scenes. BSON extends the JSON model to provide additional data types, ordered fields, and to be efficient for encoding and decoding within different languages.
MongoDB, BSON, and JSON
The MongoDB BSON implementation is lightweight, fast and highly traversable. Like JSON, MongoDB's BSON implementation supports embedding objects and arrays within other objects and arrays – MongoDB can even 'reach inside' BSON objects to build indexes and match objects against query expressions on both top-level and nested BSON keys. This means that MongoDB gives users the ease of use and flexibility of JSON documents together with the speed and richness of a lightweight binary format.
http://mongodb.github.io/mongo-csharp-driver/2.0/reference/bson/bson/
12345678910string outputFileName;
// initialize to the file to write to.
using (
var
stream = File.OpenWrite(outputFileName))
using (
var
writer = new BsonBinaryWriter(stream))
{
writer.WriteStartDocument();
writer.WriteName(
"a"
);
writer.WriteInt32(1);
writer.WriteEndDocument();
}
SON
In the same way, we can write a JSON string using a
JsonWriter
. For example, to write the document{ a: 1 }
:
12345678910string outputFileName;
// initialize to the file to write to.
using (
var
output = new StreamWriter(outputFileName))
using (
var
writer = new JsonWriter(output))
{
writer.WriteStartDocument();
writer.WriteName(
"a"
);
writer.WriteInt32(1);
writer.WriteEndDocument();
}
https://blog.csdn.net/zfskkk/article/details/78608844
查询、修改 BSON 大于JSON
总上所述:
数据结构:
json是像字符串一样存储的,bson是按结构存储的(像数组 或者说struct)存储空间
bson>json操作速度
bson>json。比如,遍历查找:json需要扫字符串,而bson可以直接定位修改:
json也要大动大移,bson就不需要。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现