原文:https://www.e-learn.cn/content/qita/1999197html
https://baike.baidu.com/item/BSONgit
概念
編輯github
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/mongodb
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).express
BSON was designed to have the following three characteristics:json
Lightweight
Keeping spatial overhead to a minimum is important for any data representation format, especially when used over the network.api
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.網絡
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就不須要。