原文: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/
string 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 }
:string 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就不需要。