mongodb data type (objectID type ; BSON / JSON type)

RDBMS 에서 데이터 속성을 표현하기 위해 data type이 존재하듯이, mongoDB에도 data type이 제공된다. (Object_ID type, JSON type)



(1) Object_ID type >>
  • RDBMS에서 제공하는 rowID (or primary key)와 유사하다. - 유일한 값.
  • 12 type binary 로 구성.
  • one collection - one document, unique value is given to Object_ID
  • 사용자가 직접 unique한 값 지정 가능. 
BSON is a binary serialization format used to store documents and make remote procedure calls in MongoDB. The BSON specification is located at bsonspec.org.
( BSON이란 documents를 저장하기 위해 사용되는 2진법 data의 나열로 몽고db에서 사용된다- )

BSON supports the following data types as values in documents. Each data type has a corresponding number and string alias that can be used with the $type operator to query documents by BSON type. (BSON은 아래와 같은 4가지 데이터 타입을 지원한다. 각 데이터 타입은 각 숫자코드를 갖고 있으며 이 숫자코드는 달러 $ 마크와 함께 붙어 query로 사용될 수 있다.)

아래는 BSON ObjectID data type이다.
C:\Users\Gin>cd C:\Program Files\MongoDB\Server\3.2\bin

C:\Program Files\MongoDB\Server\3.2\bin>mongo
2016-08-30T14:13:13.463+0900 I CONTROL  [main] Hotfix KB2731284 or later update
is not installed, will zero-out data files
MongoDB shell version: 3.2.9
connecting to: test
> use test
switched to db test
> p= { eno:1101, fname:"adam", lname:"kroll", job:"manager", salary:1000, dept_n
ame:"sales"}
{
        "eno" : 1101,
        "fname" : "adam",
        "lname" : "kroll",
        "job" : "manager",
        "salary" : 1000,
        "dept_name" : "sales"
}
> db.emp.save(p)
WriteResult({ "nInserted" : 1 })
> p
{
        "eno" : 1101,
        "fname" : "adam",
        "lname" : "kroll",
        "job" : "manager",
        "salary" : 1000,
        "dept_name" : "sales",
        "_id" : ObjectId("57c518407ddf1a9eafd2c98e") << BSON ObjectID type임. ObjectId is automatically created by mongoDB
}
> db.emp.findOne({_id: ObjectId("57c518407ddf1a9eafd2c98e")})
{
        "_id" : ObjectId("57c518407ddf1a9eafd2c98e"),
        "eno" : 1101,
        "fname" : "adam",
        "lname" : "kroll",
        "job" : "manager",
        "salary" : 1000,
        "dept_name" : "sales"
}
> db.emp.drop()
true
> db.things.find()
>





(2) JSON type >>

  • 일반적인 RDBMS처럼 문자, 숫자, binary 데이터를 저장할 수 있는 data type을 의미.
  • 아래 18가지 data type이 JSON type에 속함.

TypeNumberAlias
Double1“double”
String2“string”
Object3“object”
Array4“array”
Binary data5“binData”
Undefined6“undefined”
ObjectId7“objectId”
Boolean8“bool”
Date9“date”
Null10“null”
Regular Expression11“regex”
DBPointer12“dbPointer”
JavaScript13“javascript”
Symbol14“symbol”
JavaScript (with scope)15“javascriptWithScope”
32-bit integer16“int”
Timestamp17“timestamp”
64-bit integer18“long”
Min key-1“minKey”
Max key127“maxKey”



데이터 타입을 정의하는 방법은 아래와 같다.

> p = {"v_date": ISODate("2013-01-21T14:22:46.7772"), "v_bin":BinData(0, "2faece
s232csdceq2424"), "v_char":"Joo jongmyun", "v_num":1038641858, "v_arr": ["jin@da
um.net","jin@yahoo.com"], "v_bignum": NumberLong(125700)}
{
        "v_date" : ISODate("2013-01-21T14:22:46.777Z"),
        "v_bin" : BinData(0,"2faeces232csdceq2424"),
        "v_char" : "Joo jongmyun",
        "v_num" : 1038641858,
        "v_arr" : [
                "jin@daum.net",
                "jin@yahoo.com"
        ],
        "v_bignum" : NumberLong(125700)
}
> db.data.att.save(p)
WriteResult({ "nInserted" : 1 })

> db.data.att.find()
{ "_id" : ObjectId("57c51ebf7ddf1a9eafd2c98f"), "v_date" : ISODate("2013-01-21T1
4:22:46.777Z"), "v_bin" : BinData(0,"2faeces232csdceq2424"), "v_char" : "Joo jon
gmyun", "v_num" : 1038641858, "v_arr" : [ "jin@daum.net", "jin@yahoo.com" ], "v_
bignum" : NumberLong(125700) }





배열 데이터 타입에 관한 예제는 아래와 같다 (array type data)

> for (var p = 1103 ; p<=1110; p++) db.things.save({empno:p, ename:"test", sal:1
000})
WriteResult({ "nInserted" : 1 })

> db.things.find()
{ "_id" : ObjectId("57c51f027ddf1a9eafd2c990"), "empno" : 1103, "ename" : "test"
, "sal" : 1000 }
{ "_id" : ObjectId("57c51f027ddf1a9eafd2c991"), "empno" : 1104, "ename" : "test"
, "sal" : 1000 }
{ "_id" : ObjectId("57c51f027ddf1a9eafd2c992"), "empno" : 1105, "ename" : "test"
, "sal" : 1000 }
{ "_id" : ObjectId("57c51f027ddf1a9eafd2c993"), "empno" : 1106, "ename" : "test"
, "sal" : 1000 }
{ "_id" : ObjectId("57c51f027ddf1a9eafd2c994"), "empno" : 1107, "ename" : "test"
, "sal" : 1000 }
{ "_id" : ObjectId("57c51f027ddf1a9eafd2c995"), "empno" : 1108, "ename" : "test"
, "sal" : 1000 }
{ "_id" : ObjectId("57c51f027ddf1a9eafd2c996"), "empno" : 1109, "ename" : "test"
, "sal" : 1000 }
{ "_id" : ObjectId("57c51f027ddf1a9eafd2c997"), "empno" : 1110, "ename" : "test"
, "sal" : 1000 }

> var cursor = db.things.find()
> while (cursor.hasNext()) printjson(cursor.next())
{
        "_id" : ObjectId("57c51f027ddf1a9eafd2c990"),
        "empno" : 1103,
        "ename" : "test",
        "sal" : 1000
}
{
        "_id" : ObjectId("57c51f027ddf1a9eafd2c991"),
        "empno" : 1104,
        "ename" : "test",
        "sal" : 1000
}
{
        "_id" : ObjectId("57c51f027ddf1a9eafd2c992"),
        "empno" : 1105,
        "ename" : "test",
        "sal" : 1000
}
{
        "_id" : ObjectId("57c51f027ddf1a9eafd2c993"),
        "empno" : 1106,
        "ename" : "test",
        "sal" : 1000
}
{
        "_id" : ObjectId("57c51f027ddf1a9eafd2c994"),
        "empno" : 1107,
        "ename" : "test",
        "sal" : 1000
}
{
        "_id" : ObjectId("57c51f027ddf1a9eafd2c995"),
        "empno" : 1108,
        "ename" : "test",
        "sal" : 1000
}
{
        "_id" : ObjectId("57c51f027ddf1a9eafd2c996"),
        "empno" : 1109,
        "ename" : "test",
        "sal" : 1000
}
{
        "_id" : ObjectId("57c51f027ddf1a9eafd2c997"),
        "empno" : 1110,
        "ename" : "test",
        "sal" : 1000
}

> var cursor = db.things.find()
> printjson(cursor[7])
{
        "_id" : ObjectId("57c51f027ddf1a9eafd2c997"),
        "empno" : 1110,
        "ename" : "test",
        "sal" : 1000
}

> var arr = db.things.find().toArray()
> arr[7]
{
        "_id" : ObjectId("57c51f027ddf1a9eafd2c997"),
        "empno" : 1110,
        "ename" : "test",
        "sal" : 1000
}
> db.things.drop()
true
>




시간 데이터 타입에 관한 예제는 아래와 같다 (date / timestamp - milisecond )

C:\Users\Gin>cd C:\Program Files\MongoDB\Server\3.2\bin

C:\Program Files\MongoDB\Server\3.2\bin>mongo
2016-08-30T15:39:25.586+0900 I CONTROL  [main] Hotfix KB2731284 or later update
is not installed, will zero-out data files
MongoDB shell version: 3.2.9
connecting to: test

> x = new Date()
ISODate("2016-08-30T06:39:34.463Z") >> it's Date type.

> x.toString()
Tue Aug 30 2016 15:39:34 GMT+0900   >> it's turned into String type now.

> d=ISODate()
ISODate("2016-08-30T06:39:47.953Z")   >> present time is turned into Date type using ISODate function.

> d.getMonth()
7   >> current month - 1 

> d.getYear()
116     >> 2000 year = 100,  2016= 116

> db.foo.insert({x:1, y:new Timestamp()})
WriteResult({ "nInserted" : 1 })

> db.foo.find()
{ "_id" : ObjectId("57c52a75d3f1ba0b4c2f4c10"), "x" : 1, "y" : Timestamp(1472539
253, 1) }   >> timestamp (get milisecond, timestamp 필드 정보를 기준으로 실행.)

> db.foo.drop()
true

> db.foo.insert({y:new Timestamp(), x:3})
WriteResult({ "nInserted" : 1 })

> db.foo.find({}, {_id:0})
{ "y" : Timestamp(1472539293, 1), "x" : 3 }

> for (var p=0; p<10; p++) db.foo.insert({y:new Timestamp(), x:p})
WriteResult({ "nInserted" : 1 })
> db.foo.find({},{_id:0})
{ "y" : Timestamp(1472539293, 1), "x" : 3 }
{ "y" : Timestamp(1472539357, 1), "x" : 0 }
{ "y" : Timestamp(1472539357, 2), "x" : 1 }
{ "y" : Timestamp(1472539357, 3), "x" : 2 }
{ "y" : Timestamp(1472539357, 4), "x" : 3 }
{ "y" : Timestamp(1472539357, 5), "x" : 4 }
{ "y" : Timestamp(1472539357, 6), "x" : 5 }
{ "y" : Timestamp(1472539357, 7), "x" : 6 }
{ "y" : Timestamp(1472539357, 8), "x" : 7 }
{ "y" : Timestamp(1472539357, 9), "x" : 8 }
{ "y" : Timestamp(1472539357, 10), "x" : 9 }
> C:\Program Files\MongoDB\Server\3.2\bin



RDBMS 에 있는 seq는 mongoDB에는 없다.  하지만 함수를 만들어 해결할 수 있다.
아래는 그 함수를 보여준다.

function seq_no(name){
    var ret = db.seq_no.findAndModify(
            {query:{_id:name},
            update:{$inc:{next:1}},
            "new":true, upsert:true});
        return ret.next;
    }
    
db.order_no.insert({_id:seq_no("order_no"), name:"Jimy"})
db.order_no.insert({_id:seq_no("order_no"), name:"Chad"})

db.order_no.find()

<< 결과
Previous
Next Post »