1. 程式人生 > >BSON及mongoDB資料型別

BSON及mongoDB資料型別

轉自Leshami大佬       原文連結:https://blog.csdn.net/robinson_0612/article/details/52668870

 

    JSON是一種被廣泛使用的輕量級的資料交換格式,支援現今絕大多數主流的開發語言。而近幾年崛起的mongDB則採用了類JSON的資料格式,在JSON之上進行了豐富和增強,使得mongoDB可以處理及報錯更大的資料型別。本文就2者進行描述同時給出mongoDB支援的資料型別。

一、JSON特性

1、什麼是JSON
        JSON(JavaScript Object Notation) 是一種輕量級的資料交換格式。
        JSON採用完全獨立於語言的文字格式,但也使用了類似於C語言家族的習慣(包括C、C++、C#、Java、JavaScript、Perl、Python等)。
        這些特性使JSON成為理想的資料交換語言。 易於人閱讀和編寫,同時也易於機器解析和生成(一般用於提升網路傳輸速率)。
        可以詳細參考:http://www.json.org.cn/

2、JSON 資料的書寫格式
        名稱/值對 物件
        是一個無序的“‘名稱/值’對”集合。一個物件以“{”(左括號)開始,“}”(右括號)結束。
        每個“名稱”後跟一個“:”(冒號);“‘名稱/值’ 對”之間使用“,”(逗號)分隔。
        如:  ({"firstName":"John"}),更多的格式見本文第而部分BSON支援的資料型別
        等價於這條 JavaScript 語句: firstName="John"

3、JSON僅支援以下資料型別
        數字(整數或浮點數)
        字串(在雙引號中)
        邏輯值(true 或 false)
        陣列(在方括號中)
        物件(在花括號中)
        null

4、JSON基於兩種結構:
        “名稱/值”對的集合(A collection of name/value pairs),在不同的程式語言中有不同的描述
                    如:物件(object),紀錄(record),結構(struct),字典(dictionary)
                    雜湊表(hash table),有鍵列表(keyed list),或者關聯陣列 (associative array)
        值的有序列表。在大部分語言中,它被實現為陣列(array),向量(vector),列表(list),序列(sequence)

 

二、BSON特性

1、什麼是BSON
        BSON()是一種類json的一種二進位制形式的儲存格式,簡稱Binary JSON
        它和JSON一樣,支援內嵌的文件物件和陣列物件,但是BSON有JSON沒有的一些資料型別,如Date和BinData型別。
        https://docs.mongodb.com/manual/reference/bson-types/

2、BSON的特性
    輕量性、可遍歷性、高效性

3、mongoDB與BSON
        mongoDB對JSON串做了一些增加,使其可以支援更多的資料型別,並且將其作為儲存結構
        mongoDB這種格式轉化成一文件這個概念(Document),因為BSON是schema-free的,所以在MongoDB中所對應的文件也有這個特徵
        mongoDB以BSON做為其儲存結構的一種重要原因是其可遍歷性

4、演示mongoDB支援的資料型別
//null值

    db.mycol.insert({x:null})
    WriteResult({ “nInserted” : 1 })

//布林型,用於儲存布林值(真/假)

    db.mycol.insert({x:true})
    WriteResult({ “nInserted” : 1 })

//小數

    db.mycol.insert({x:3.1515})
    WriteResult({ “nInserted” : 1 })

//整數,用於儲存數值。根據你所採用的伺服器,可分為 32 位或 64 位

    db.mycol.insert({x:3})
    WriteResult({ “nInserted” : 1 })

//4位元組帶符合整數

    db.mycol.insert({x:NumberInt(“3”)})
    WriteResult({ “nInserted” : 1 })

//8位元組帶符號整數

    db.mycol.insert({x:NumberLong(“3”)})
    WriteResult({ “nInserted” : 1 })

//字元型,儲存資料常用的資料型別。在 MongoDB 中,UTF-8 編碼的字串才是合法的

    db.mycol.insert({x:”robin”})
    WriteResult({ “nInserted” : 1 })

//日期型

    db.mycol.insert({x:new Date()})
    WriteResult({ “nInserted” : 1 })

//正則表示式

    db.mycol.insert({x:/u01/i})
    WriteResult({ “nInserted” : 1 })

//陣列,用於將陣列或列表或多個值儲存為一個鍵

    db.mycol.insert({x:[“a”,”b”,”c”]})
    WriteResult({ “nInserted” : 1 })

//巢狀文件,用於內嵌文件

    db.mycol.insert({x:{y:”nested”}})
    WriteResult({ “nInserted” : 1 })

//物件id

    db.mycol.insert({x:ObjectId()})
    WriteResult({ “nInserted” : 1 })

//程式碼段

    db.mycol.insert({x:function(){/This is a test code/}})
    WriteResult({ “nInserted” : 1 })

//查詢集合mycol的文件

    db.mycol.find({},{_id:0})
    { “x” : null }
    { “x” : true }
    { “x” : 3.1515 }
    { “x” : 3 } //Author : Leshami
    { “x” : 3 } //Blog : http://blog.csdn.net/leshami
    { “x” : NumberLong(3) }
    { “x” : “robin” }
    { “x” : ISODate(“2016-09-06T02:46:35.173Z”) }
    { “x” : /u01/i }
    { “x” : [ “a”, “b”, “c” ] }
    { “x” : { “y” : “nested” } }
    { “x” : ObjectId(“57ce2f34ce8685a6fd9df3ae”) }
    { “x” : function (){/This is a test code/} }

//undefined型別

    db.mycol.insert({name:undefined});
    WriteResult({ “nInserted” : 1 })
    db.mycol.insert({name:”123”});
    WriteResult({ “nInserted” : 1 })
    printjson(db.mycol.find().toArray())
    [
    {
    “_id” : ObjectId(“57ce1cc8c60f1fe489e49c68”),
    “name” : undefined
    },
    {
    “_id” : ObjectId(“57ce1cdbc60f1fe489e49c69”),
    “name” : “123”
    }
    ]

//查詢undefined型別的文件

    db.mycol.find({name:”undefined”})
    db.mycol.find({name:undefined})
    Error: error: {
    “waitedMS” : NumberLong(0),
    “ok” : 0,
    “errmsg” : “cannot compare to undefined”,
    “code” : 2
    }

    db.mycol.find({name:{$type:6}})
    { “_id” : ObjectId(“57ce1cc8c60f1fe489e49c68”), “name” : undefined }

    db.mycol.find({name:{$type:”undefined”}})
    { “_id” : ObjectId(“57ce1cc8c60f1fe489e49c68”), “name” : undefined }

//mongoDB資料型別的比較與排序優先順序
1) MinKey (internal type)
2) Null
3) Numbers (ints, longs, doubles)
4) Symbol, String
5) Object
6) Array
7) BinData
8) ObjectId
9) Boolean
10) Date
11) Timestamp
12) Regular Expression
13) MaxKey (internal type)

5、關於_id與Object_Id
mongoDB中每一個文件都必須有一個"_id"鍵,該鍵等同於RDBMS中的主鍵,只不過這個主鍵是由mongoDB自動生成
"_id"鍵的值可以使用任意型別,可以不使用系統建立,而由使用者自定義的規則生成
"_id"為輕量級,全域性唯一,可類比為MySQL資料中的GTID,也用於解決不同機器副本集複製時唯一性問題

a 4-byte value representing the seconds since the Unix epoch,  //時間戳
a 3-byte machine identifier,                                   //機器唯一標識碼
a 2-byte process id, and                                       //程序ID
a 3-byte counter, starting with a random value.                //隨機數

 

    db.mycol.findOne()
    { “_id” : ObjectId(“57ce2d4cce8685a6fd9df3a3”), “x” : null }
    57ce2d4c //時間戳 ==>1473129804 ==> 2016/9/6 10:43:24
    ce8685 //機器唯一標識碼
    a6fd //程序ID
    9df3a3 //隨機數