什么是 BSON,它与 JSON 究竟有何不同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12438280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:31:46  来源:igfitidea点击:

What is BSON and exactly how is it different from JSON?

jsonmongodbbsonnosql

提问by Akshat Jiwan Sharma

I am just starting out with MongoDB and one of the things that I have noticed is that it uses BSONto store data internally. However the documentation is not exactly clear on what BSON is and how it is used in MongoDB. Can someone explain it to me, please?

我刚开始使用 MongoDB,我注意到的一件事是它使用BSON在内部存储数据。然而,文档并不清楚 BSON 是什么以及它如何在 MongoDB 中使用。有人可以向我解释一下吗?

采纳答案by JohnnyHK

BSONis the binary encoding of JSON-like documents that MongoDB uses when storing documents in collections. It adds support for data types like Date and binary that aren't supported in JSON.

BSON是 MongoDB 在集合中存储文档时使用的类 JSON 文档的二进制编码。它增加了对 JSON 不支持的数据类型(如 Date 和 binary)的支持。

In practice, you don't have to know much about BSON when working with MongoDB, you just need to use the native types of your language and the supplied types (e.g. ObjectId) of its driver when constructing documents and they will be mapped into the appropriate BSON type by the driver.

在实践中,在使用 MongoDB 时,您不必对 BSON 了解太多,您只需要在构建文档时使用您语言的本机类型及其驱动程序提供的类型(例如 ObjectId),它们就会被映射到驱动程序适当的 BSON 类型。

回答by Mehdi Raash

  • What's BSON?

    BSON [bee · sahn], short for Bin-ary JSON, is a bin-ary-en-coded seri-al-iz-a-tion of JSON-like doc-u-ments.

  • How is it different from JSON?

    BSON is designed to be efficient in space, but in some cases is not much more efficient than JSON. In some cases BSON uses even more space than JSON. The reason for this is another of the BSON design goals: traversability. BSON adds some "extra" information to documents, like length of strings and subobjects. This makes traversal faster.

    BSON is also designed to be fast to encode and decode. For example, integers are stored as 32 (or 64) bit integers, so they don't need to be parsed to and from text. This uses more space than JSON for small integers, but is much faster to parse.

    In addition to compactness, BSON adds additional data types unavailable in JSON, notably the BinDataand Datedata types.

  • 什么是BSON

    BSON [bee·sahn],Bin-ary JSON 的缩写,是一个类似 JSON 的文档的二进制编码序列。

  • 它与JSON有何不同?

    BSON 被设计为在空间上高效,但在某些情况下并不比 JSON 高效多少。在某些情况下,BSON 使用的空间甚至比 JSON 还要多。这样做的原因是 BSON 的另一个设计目标:可遍历性。BSON 向文档添加了一些“额外”信息,例如字符串和子对象的长度。这使得遍历更快。

    BSON 还被设计为快速编码和解码。例如,整数存储为 32(或 64)位整数,因此它们不需要与文本进行解析。对于小整数,这比 JSON 使用更多的空间,但解析速度要快得多。

    除了紧凑性之外,BSON 还添加了 JSON 中不可用的其他数据类型,特别是BinDataDate数据类型。

Source: http://bsonspec.org/

资料来源:http: //bsonspec.org/

回答by codergirl22

MongoDB represents JSON documents in binary-encoded format called BSON behind the scenes. BSON extends the JSON model to provide additional data types and to be efficient for encoding and decoding within different languages.

MongoDB 在后台以称为 BSON 的二进制编码格式表示 JSON 文档。BSON 扩展了 JSON 模型以提供额外的数据类型,并在不同语言中高效地进行编码和解码。

回答by Avinash Maurya

MongoDB represents JSON documentsin binary-encoded formatso we call it BSON behind the scenes.

MongoDB以二进制编码格式表示JSON 文档,因此我们在后台将其称为BSON

BSON extends the JSON model to provide additional data types such? as Date and binarywhich are not supported in JSONalso provide ordered fields in order for it to be efficient for encoding and decoding within different languages.?

BSON 扩展了 JSON 模型以提供额外的数据类型,例如?因为JSON中不支持的日期和二进制文件也提供了有序字段,以便在不同语言中高效地进行编码和解码。?

In other word we can say??BSON?is just binary?JSON?( a superset of?JSON?with some more data types, most importantly binary byte array ).

换句话说,我们可以说??BSON?只是二进制?JSON?(JSON的超集?具有更多数据类型,最重要的是二进制字节数组)。

Mongodb using as a?serialization format of JSONinclude with encoding format for storing and accessing documents. simply we can say?BSON?is a binary encoded format for?JSON?data.

Mongodb 用作 JSON 的序列化格式,包括用于存储和访问文档的编码格式。我们可以简单地说?BSON?是?JSON?数据的二进制编码格式。

for more mongoDB Article : https://om9x.com/blog/bson-vs-json/

更多 mongoDB 文章:https://om9x.com/blog/bson-vs-json/

回答by Anupam Mahapatra

By using BSON encoding on top of JSON, MongoDB gets the capability of creating indexes on top of values that resides inside the JSON document in raw format. This helps in running efficient analytical queries as NoSQL system were known for having no support for Indexes.

通过在 JSON 之上使用 BSON 编码,MongoDB 能够在原始格式的 JSON 文档中的值之上创建索引。这有助于运行高效的分析查询,因为众所周知 NoSQL 系统不支持索引。

回答by HairOfTheDog

This relatively short article gives a pretty good explanation of BSON and JSON: It talks about some of the problems with JSON, why BSON was invented, what problems it solves compared to JSON and how it could benefit you.

这篇相对较短的文章很好地解释了 BSON 和 JSON:它讨论了 JSON 的一些问题、为什么发明 BSON、与 JSON 相比它解决了哪些问题以及它如何使您受益。

https://www.compose.com/articles/from-json-to-bson-and-back/

https://www.compose.com/articles/from-json-to-bson-and-back/

In my use case that article told me that serializing to JSON would work for me and I didn't need to serialize to BSON

在我的用例中,那篇文章告诉我序列化为 JSON 对我有用,我不需要序列化为 BSON