database/mongodb

[mongodb] 강좌5 BigData MongoDB(NoSQL)

labj 2014. 3. 3. 01:09
[mongodb] 강좌4 BigData MongoDB(NoSQL)


 

- upsert Flag

db.collection.update( <query>,

                      <update>,

                      { upsert: true } )

 upsert: true  : 아이디가 같은데 업데이트 다른다면 insert 한다.

 

 

db.bios.update(

   { name: { first: 'Dennis', last: 'Ritchie'} },

   {

     name: { first: 'Dennis', last: 'Ritchie'},

     birth: new Date('Sep 09, 1941'),

     death: new Date('Oct 12, 2011'),

     contribs: [ 'UNIX', 'C' ],

     awards: [

               {

                 award: 'Turing Award',

                 year: 1983,

                 by: 'ACM'

               },

               {

                 award: 'National Medal of Technology',

                 year: 1998,

                 by: 'United States'

               },

               {

                 award: 'Japan Prize',

                 year: 2011,

                 by: 'The Japan Prize Foundation'

               }

             ]

   },

   { upsert: true }

)

 

> db.bios.find({name:{first:'Dennis',last:'Ritchie'}}).toArray();

[

        {

                "_id" : ObjectId("52eedec30aa7a3e1b31ecbdf"),

                "name" : {

                        "first" : "Dennis",

                        "last" : "Ritchie"

                },

                "birth" : ISODate("1941-09-08T15:00:00Z"),

                "death" : ISODate("2011-10-11T15:00:00Z"),

                "contribs" : [

                        "UNIX",

                        "C"

                ],

                "awards" : [

                        {

                                "award" : "Turing Award",

                                "year" : 1983,

                                "by" : "ACM"

                        },

                        {

                                "award" : "National Medal of Technology",

                                "year" : 1998,

                                "by" : "United States"

                        },

                        {

                                "award" : "Japan Prize",

                                "year" : 2011,

                                "by" : "The Japan Prize Foundation"

                        }

                ]

        }

]

 

> db.bios.find(

...    {

...       _id: { $in: [ 5, 3] }

...    }

... ).toArray();

[

        {

                "_id" : 3,

                "name" : {

                        "first" : "Grace",

                        "last" : "Hopper"

                },

                "title" : "Rear Admiral",

                "birth" : ISODate("1906-12-08T15:00:00Z"),

                "death" : ISODate("1991-12-31T15:00:00Z"),

                "contribs" : [

                        "UNIVAC",

                        "compiler",

                        "FLOW-MATIC",

                        "COBOL"

                ],

                "awards" : [

                        {

                                "award" : "Computer Sciences Man of the Year",

                                "year" : 1969,

                                "by" : "Data Processing Management Association"

                        },

                        {

                                "award" : "Distinguished Fellow",

                                "year" : 1973,

                                "by" : " British Computer Society"

                        },

                        {

                                "award" : "W. W. McDowell Award",

                                "year" : 1976,

                                "by" : "IEEE Computer Society"

                        },

                        {

                                "award" : "National Medal of Technology",

                                "year" : 1991,

                                "by" : "United States"

                        }

                ]

        },

        {

                "_id" : 5,

                "name" : {

                        "first" : "Ole-Johan",

                        "last" : "Dahl"

                },

                "birth" : ISODate("1931-10-11T15:00:00Z"),

                "death" : ISODate("2002-06-28T15:00:00Z"),

                "contribs" : [

                        "OOP",

                        "Simula"

                ],

                "awards" : [

                        {

                                "award" : "Rosing Prize",

                                "year" : 1999,

                                "by" : "Norwegian Data Association"

                        },

                        {

                                "award" : "Turing Award",

                                "year" : 2001,

                                "by" : "ACM"

                        },

                        {

                                "award" : "IEEE John von Neumann Medal",

                                "year" : 2001,

                                "by" : "IEEE"

                        }

                ]

        }

]

 

> db.bios.find(

...    {

...       awards: {

...                 $elemMatch: {

...                      award: 'Turing Award',

...                      year: { $gt: 1980 }

...                 }

...       }

...    }

... ).toArray();

[

        {

                "_id" : 4,

                "name" : {

                        "first" : "Kristen",

                        "last" : "Nygaard"

                },

                "birth" : ISODate("1926-08-26T15:00:00Z"),

                "death" : ISODate("2002-08-09T15:00:00Z"),

                "contribs" : [

                        "OOP",

                        "Simula"

                ],

                "awards" : [

                        {

                                "award" : "Rosing Prize",

                                "year" : 1999,

                                "by" : "Norwegian Data Association"

                        },

                        {

                                "award" : "Turing Award",

                                "year" : 2001,

                                "by" : "ACM"

                        },

                        {

                                "award" : "IEEE John von Neumann Medal",

                                "year" : 2001,

                                "by" : "IEEE"

                        }

                ]

        },

        {

                "_id" : 5,

                "name" : {

                        "first" : "Ole-Johan",

                        "last" : "Dahl"

                },

                "birth" : ISODate("1931-10-11T15:00:00Z"),

                "death" : ISODate("2002-06-28T15:00:00Z"),

                "contribs" : [

                        "OOP",

                        "Simula"

                ],

                "awards" : [

                        {

                                "award" : "Rosing Prize",

                                "year" : 1999,

                                "by" : "Norwegian Data Association"

                        },

                        {

                                "award" : "Turing Award",

                                "year" : 2001,

                                "by" : "ACM"

                        },

                        {

                                "award" : "IEEE John von Neumann Medal",

                                "year" : 2001,

                                "by" : "IEEE"

                        }

                ]

        },

        {

                "_id" : ObjectId("52eedec30aa7a3e1b31ecbdf"),

                "name" : {

                        "first" : "Dennis",

                        "last" : "Ritchie"

                },

                "birth" : ISODate("1941-09-08T15:00:00Z"),

                "death" : ISODate("2011-10-11T15:00:00Z"),

                "contribs" : [

                        "UNIX",

                        "C"

                ],

                "awards" : [

                        {

                                "award" : "Turing Award",

                                "year" : 1983,

                                "by" : "ACM"

                        },

                        {

                                "award" : "National Medal of Technology",

                                "year" : 1998,

                                "by" : "United States"

                        },

                        {

                                "award" : "Japan Prize",

                                "year" : 2011,

                                "by" : "The Japan Prize Foundation"

                        }

                ]

        }

]

 

> db.bios.find(

...    {

...      'name.first': 'Grace',

...      'name.last': 'Hopper'

...    }

... ).toArray();

[

        {

                "_id" : 3,

                "name" : {

                        "first" : "Grace",

                        "last" : "Hopper"

                },

                "title" : "Rear Admiral",

                "birth" : ISODate("1906-12-08T15:00:00Z"),

                "death" : ISODate("1991-12-31T15:00:00Z"),

                "contribs" : [

                        "UNIVAC",

                        "compiler",

                        "FLOW-MATIC",

                        "COBOL"

                ],

                "awards" : [

                        {

                                "award" : "Computer Sciences Man of the Year",

                                "year" : 1969,

                                "by" : "Data Processing Management Association"

                        },

                        {

                                "award" : "Distinguished Fellow",

                                "year" : 1973,

                                "by" : " British Computer Society"

                        },

                        {

                                "award" : "W. W. McDowell Award",

                                "year" : 1976,

                                "by" : "IEEE Computer Society"

                        },

                        {

                                "award" : "National Medal of Technology",

                                "year" : 1991,

                                "by" : "United States"

                        }

                ]

        }

]

> db.bios.find(

...     { },

...     { name: 1, contribs: 1 }

...  ).toArray();

[

        {

                "_id" : 1,

                "name" : {

                        "first" : "John",

                        "last" : "Backus"

                },

                "contribs" : [

                        "Fortran",

                        "ALGOL",

                        "Backus-Naur Form",

                        "FP"

                ]

        },

        {

                "_id" : ObjectId("52e060dee4d16089c04c02e9"),

                "name" : {

                        "first" : "John",

                        "last" : "McCarthy"

                },

                "contribs" : [

                        "Lisp",

                        "Artificial Intelligence",

                        "ALGOL"

                ]

        },

        {

                "_id" : 3,

                "name" : {

                        "first" : "Grace",

                        "last" : "Hopper"

                },

                "contribs" : [

                        "UNIVAC",

                        "compiler",

                        "FLOW-MATIC",

                        "COBOL"

                ]

        },

        {

                "_id" : 4,

                "name" : {

                        "first" : "Kristen",

                        "last" : "Nygaard"

                },

                "contribs" : [

                        "OOP",

                        "Simula"

                ]

        },

        {

                "_id" : 5,

                "name" : {

                        "first" : "Ole-Johan",

                        "last" : "Dahl"

                },

                "contribs" : [

                        "OOP",

                        "Simula"

                ]

        },

        {

                "_id" : ObjectId("52e0631ee4d16089c04c02eb"),

                "name" : {

                        "first" : "Guido",

                        "last" : "van Rossum"

                },

                "contribs" : [

                        "Python"

                ]

        },

        {

                "_id" : ObjectId("52eedec30aa7a3e1b31ecbdf"),

                "name" : {

                        "first" : "Dennis",

                        "last" : "Ritchie"

                },

                "contribs" : [

                        "UNIX",

                        "C"

                ]

        }

]

 

jayData 홈페이지 Manage data in JavaScript

http://jaydata.org/

 

jData 배워야 한다. 중요하다.

restful data jData 이용해서 한다.

 

var todoDB = new TodoDatabase({

    provider: 'mongoDB' , databaseName: 'MyTodoDatabase'

});

 

todoDB.onReady(function() {

    //Work with todoDB now

});

 

http://www.10gen.com

 

 

 

 

 

> db

tests

> show dbs

local   0.078125GB

mydb    0.203125GB

personDB        0.203125GB

persons 0.203125GB

test    0.203125GB

tests   0.203125GB

> show collections

bios

system.indexes

 

mongodb에서는 data insert하면 ObjectId가 만들어진다.

 

> db.users.insert({age:11});

> db.users.insert({age:22});

> db.users.find().toArray();

[

        {

                "_id" : ObjectId("52f0c5f6a917b125e64eca38"),

                "age" : 11

        },

        {

                "_id" : ObjectId("52f0c5fea917b125e64eca39"),

                "age" : 22

        }

]

 

ObjectId 변수에 담아본다.

> var id = db.users.find()[0]._id;

> id

ObjectId("52f0c5f6a917b125e64eca38")

 

새로운 UUID 만들 있다.

> new ObjectId

ObjectId("52f0c6d5a917b125e64eca3a")

> new ObjectId

ObjectId("52f0c6d8a917b125e64eca3b")

> new ObjectId

ObjectId("52f0c6d9a917b125e64eca3c")

 

join table에서 id primary key 역할을 한다.

> id=db.users.find()[0]._id

ObjectId("52f0c5f6a917b125e64eca38")

> id2=db.users.find()[1]._id

ObjectId("52f0c5fea917b125e64eca39")

> show collections

bios

system.indexes

users

> db.persons.insert({name:'namju1',userId:id });

> db.persons.insert({name:'namju2',userId:id2 });

> show collections

bios

persons

system.indexes

users

> db.persons.find();

{ "_id" : ObjectId("52f0c76ca917b125e64eca3d"), "name" : "namju1", "userId" : Ob

jectId("52f0c5f6a917b125e64eca38") }

{ "_id" : ObjectId("52f0c773a917b125e64eca3e"), "name" : "namju2", "userId" : Ob

jectId("52f0c5fea917b125e64eca39") }

> db.persons.find()[0].name

namju1

> db.persons.find()[0].userId

ObjectId("52f0c5f6a917b125e64eca38")

 

pretty(), toArray() 대해서 알아본다.

> db.people.insert({

...      order_id: 109384,

...      order_date: new Date("12/04/2010"),

...      customer: {

...          name: "Joe Bloggs",

...          company: "XYZ Inc.",

...          phone: "(555) 123-4567"

...      },

...      payment: {

...          type: "Cash",

...          amount: 4075.99,

...          paid_in_full: true

...      },

...      items: [

...          {

...              sku: "ABC1200",

...              description: "A sample product",

...              quantity: 1,

...              price_per_unit: 75.99,

...          }, {

...              sku: "XYZ3400",

...              description: "An expensive product",

...              quantity: 2,

...              price_per_unit: 2000

...          }

...      ],

...      cashier_id: 340582242

... });

 

스트림을 pretty()로 보여주는 것이고

toArray() restful 형태의 data하고 붙이는 것이다.

 

> db.people.find();

{ "_id" : ObjectId("52f0c84ba917b125e64eca3f"), "order_id" : 109384, "order_date

" : ISODate("2010-12-03T15:00:00Z"), "customer" : { "name" : "Joe Bloggs", "comp

any" : "XYZ Inc.", "phone" : "(555) 123-4567" }, "payment" : { "type" : "Cash",

"amount" : 4075.99, "paid_in_full" : true }, "items" : [        {       "sku" :

"ABC1200",      "description" : "A sample product",     "quantity" : 1,

"price_per_unit" : 75.99 },     {       "sku" : "XYZ3400",      "description" :

"An expensive product",         "quantity" : 2,         "price_per_unit" : 2000

} ], "cashier_id" : 340582242 }

> db.people.find().pretty();

{

        "_id" : ObjectId("52f0c84ba917b125e64eca3f"),

        "order_id" : 109384,

        "order_date" : ISODate("2010-12-03T15:00:00Z"),

        "customer" : {

                "name" : "Joe Bloggs",

                "company" : "XYZ Inc.",

                "phone" : "(555) 123-4567"

        },

        "payment" : {

                "type" : "Cash",

                "amount" : 4075.99,

                "paid_in_full" : true

        },

        "items" : [

                {

                        "sku" : "ABC1200",

                        "description" : "A sample product",

                        "quantity" : 1,

                        "price_per_unit" : 75.99

                },

                {

                        "sku" : "XYZ3400",

                        "description" : "An expensive product",

                        "quantity" : 2,

                        "price_per_unit" : 2000

                }

        ],

        "cashier_id" : 340582242

}

> db.people.find().toArray();

[

        {

                "_id" : ObjectId("52f0c84ba917b125e64eca3f"),

                "order_id" : 109384,

                "order_date" : ISODate("2010-12-03T15:00:00Z"),

                "customer" : {

                        "name" : "Joe Bloggs",

                        "company" : "XYZ Inc.",

                        "phone" : "(555) 123-4567"

                },

                "payment" : {

                        "type" : "Cash",

                        "amount" : 4075.99,

                        "paid_in_full" : true

                },

                "items" : [

                        {

                                "sku" : "ABC1200",

                                "description" : "A sample product",

                                "quantity" : 1,

                                "price_per_unit" : 75.99

                        },

                        {

                                "sku" : "XYZ3400",

                                "description" : "An expensive product",

                                "quantity" : 2,

                                "price_per_unit" : 2000

                        }

                ],

                "cashier_id" : 340582242

        }

]

 

게임업체는 웹앱을 엘리먼트 단위로 작업된다.

DB 100만명이 달라붙었는데 100만개의 프로세스를 생긴다면 관리해야 하는데

스토리지 하나에 붙게되면 관리할 필요가 없어진다.

 

> var chars = "abcdefghijklmnopqrstuvwxyz"

> for(var i=0;i<chars.length;i++) {

...     var char = chars.substr(i,1);

...     var doc = {char:char, code:char.charCodeAt(0)};

...     db.alphabet.save(doc);

... }

> show collections;

alphabet

bios

people

persons

system.indexes

users

> db.alphabet.find();

{ "_id" : ObjectId("52f0ca1ea917b125e64eca40"), "char" : "a", "code" : 97 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca41"), "char" : "b", "code" : 98 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca42"), "char" : "c", "code" : 99 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca43"), "char" : "d", "code" : 100 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca44"), "char" : "e", "code" : 101 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca45"), "char" : "f", "code" : 102 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca46"), "char" : "g", "code" : 103 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca47"), "char" : "h", "code" : 104 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca48"), "char" : "i", "code" : 105 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca49"), "char" : "j", "code" : 106 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca4a"), "char" : "k", "code" : 107 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca4b"), "char" : "l", "code" : 108 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca4c"), "char" : "m", "code" : 109 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca4d"), "char" : "n", "code" : 110 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca4e"), "char" : "o", "code" : 111 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca4f"), "char" : "p", "code" : 112 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca50"), "char" : "q", "code" : 113 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca51"), "char" : "r", "code" : 114 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca52"), "char" : "s", "code" : 115 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca53"), "char" : "t", "code" : 116 }

Type "it" for more

> it

{ "_id" : ObjectId("52f0ca1ea917b125e64eca54"), "char" : "u", "code" : 117 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca55"), "char" : "v", "code" : 118 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca56"), "char" : "w", "code" : 119 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca57"), "char" : "x", "code" : 120 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca58"), "char" : "y", "code" : 121 }

{ "_id" : ObjectId("52f0ca1ea917b125e64eca59"), "char" : "z", "code" : 122 }

 

> db.alphabet.find({},{char:1,code:1,_id:0});

{ "char" : "a", "code" : 97 }

{ "char" : "b", "code" : 98 }

{ "char" : "c", "code" : 99 }

{ "char" : "d", "code" : 100 }

{ "char" : "e", "code" : 101 }

{ "char" : "f", "code" : 102 }

{ "char" : "g", "code" : 103 }

{ "char" : "h", "code" : 104 }

{ "char" : "i", "code" : 105 }

{ "char" : "j", "code" : 106 }

{ "char" : "k", "code" : 107 }

{ "char" : "l", "code" : 108 }

{ "char" : "m", "code" : 109 }

{ "char" : "n", "code" : 110 }

{ "char" : "o", "code" : 111 }

{ "char" : "p", "code" : 112 }

{ "char" : "q", "code" : 113 }

{ "char" : "r", "code" : 114 }

{ "char" : "s", "code" : 115 }

{ "char" : "t", "code" : 116 }

Type "it" for more

> it

{ "char" : "u", "code" : 117 }

{ "char" : "v", "code" : 118 }

{ "char" : "w", "code" : 119 }

{ "char" : "x", "code" : 120 }

{ "char" : "y", "code" : 121 }

{ "char" : "z", "code" : 122 }

 

- 코드가 108번 보다 큰 값만 나오도록 한다.

이 경우에는 $를 사용한다.

http는 텍스트다. 맵프로세싱을 하는 것이다.

맵은 키값밸류이다.

컬렉션은 맵을 모아둔 것이다.

> db.alphabet.find({code:{$gt:108}},{char:1,code:1,_id:0});

{ "char" : "m", "code" : 109 }

{ "char" : "n", "code" : 110 }

{ "char" : "o", "code" : 111 }

{ "char" : "p", "code" : 112 }

{ "char" : "q", "code" : 113 }

{ "char" : "r", "code" : 114 }

{ "char" : "s", "code" : 115 }

{ "char" : "t", "code" : 116 }

{ "char" : "u", "code" : 117 }

{ "char" : "v", "code" : 118 }

{ "char" : "w", "code" : 119 }

{ "char" : "x", "code" : 120 }

{ "char" : "y", "code" : 121 }

{ "char" : "z", "code" : 122 }

 

> db.alphabet.find({char:'o'});

{ "_id" : ObjectId("52f0ca1ea917b125e64eca4e"), "char" : "o", "code" : 111 }

 

인덱스

http://www.slideshare.net/kyungseoksong/mongodb-index

인덱스를 최소화하라

 

 

 

 

[mongodb] 강좌4 BigData MongoDB(NoSQL)