# MongoDB 如何儲存資料 - BSON ## Introduction MongoDB 身為一個 NoSQL Document Database,資料會以 JSON (*JavaScript Object Notation*) 格式儲存在資料庫中,可以透過這樣一句指令取得其中的資料 ```javascript db.getCollection("user").find({}) ``` 基本上可以想成是這樣的 SQL ```sql select * from "user"; ``` 回傳結果 ```json [ { "_id": 1, "name":"timm" }, { "_id": 2, "name":"anna" } ] ``` 之中先看到我們會去對 **collection** 做查詢,而回傳的結果會是多個 **document**,並且裡面含有多個 **field** & **value**。 ### collection ```json [ { "_id": 1, "name":"timm" }, { "_id": 2, "name":"anna" } ] ``` ### document ```json { "_id": 1, "name":"timm" } ``` ### field & value | field | value | | ----- | ----- | | -id | 1 | | name | timm | ## Why JSON? 1. Friendly 2. Readable 3. Familiar JSON 身為一個輕量級的資料交換格式,單純地以下面幾項規則,就定義出了一個豐富的資料結構。就算沒有使用過 MongoDB 的工程師,大部分也都對於 JSON 格式的資料非常熟悉,並且相較於 binary 的資料,這是非常具有可讀性的。 - {} 定義一個物件 - [] 定義一個陣列 - : 區分 key & value的分隔符號 - , 不同物件、陣列之間的分隔符號 ```json { "_id": { "$oid": "56d5f7eb604eb380b0d8d8ce" }, "student_id": { "$numberDouble": "0.0" }, "scores": [ { "type": "exam", "score": { "$numberDouble": "78.40446309504266" } }, { "type": "quiz", "score": { "$numberDouble": "73.36224783231339" } } ], "class_id": { "$numberDouble": "339.0" } } ``` ## Why not JSON? 應該有注意到,整份 JSON 資料都是 **UTF-8 encoded string**,而其中不乏有些資料是數字格式,如果我們都以字串的格式存放在資料庫中,那將會非常的佔空間。 在程式中通常也不會只有 string, boolean, number, array 這樣的資料格式,但明顯的看出 JSON 格式中並無法充分的表達一些特殊格式,例如:**date**, **number(long, integer, double)** ## BSON (MongoDB 使用的資料格式) 對於人類閱讀的方便,卻不利於資料庫的空間利用,但如果全部使用 binary encoded,人類又看不懂裡面的資料會非常的困擾,於是介於 json 和 binary 資料中間,有了一個銜接兩者的存在 BSON (*Binary JSON*)。 還有其他種 binary encoded 的資料交換格式,像是:[Protocol Buffers](https://github.com/protocolbuffers/protobuf),而 BSON 的物件中包含 1. field name 2. type 3. value 定義 JSON 欄位的名稱,資料格式,以及 binary encoded value,但不用以 String 的方式儲存所以相對的就節省了非常多的空間,也有利於提升查詢速度。 範例 JSON ```json { "hello":"world" } ``` 換成用 BSON ``` \x16\x00\x00\x00 // total document size \x02 // 0x02 = type String hello\x00 // field name \x06\x00\x00\x00world\x00 // field value (size of value, value, null terminator) \x00 // 0x00 = type EOO ('end of object') ``` 所以 BSON 的採用,提供了下面的幾項優點 1. Speed 以 binary 方式儲存更有利於機器運算 2. Space binary 比起 string 使用了更少的空間 3. Flexaibility 透過 type 欄位的宣告,可以有更豐富的資料類型