【轉】BSON數據格式

原文:https://www.e-learn.cn/content/qita/1999197html

-------------------------------------------------------------

BSON

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 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).express

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

  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.api

  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就不須要。

相關文章
相關標籤/搜索