【轉】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 Bin­ary JSON, is a bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments. Like JSON, BSON sup­ports the em­bed­ding of doc­u­ments and ar­rays with­in oth­er doc­u­ments and ar­rays. BSON also con­tains ex­ten­sions that al­low rep­res­ent­a­tion of data types that are not part of the JSON spec. For ex­ample, BSON has a Date type and a BinData type.

BSON can be com­pared to bin­ary inter­change for­mats, like Proto­col Buf­fers. BSON is more "schema-less" than Proto­col Buf­fers, which can give it an ad­vant­age in flex­ib­il­ity but also a slight dis­ad­vant­age in space ef­fi­ciency (BSON has over­head for field names with­in the seri­al­ized data).

BSON was de­signed to have the fol­low­ing three char­ac­ter­ist­ics:

  1. Lightweight

    Keep­ing spa­tial over­head to a min­im­um is im­port­ant for any data rep­res­ent­a­tion format, es­pe­cially when used over the net­work.

  2. Traversable

    BSON is de­signed to be tra­versed eas­ily. This is a vi­tal prop­erty in its role as the primary data rep­res­ent­a­tion for Mon­goDB.

  3. Efficient

    En­cod­ing data to BSON and de­cod­ing from BSON can be per­formed very quickly in most lan­guages 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就不需要。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM