SlideShare una empresa de Scribd logo
1 de 38
Descargar para leer sin conexión
SWT Tech-Share
  MongoDB
  KORKEAT WANNAPAT
Agenda
➔
 NoSQL
➔
 Know MongoDB
➔
 BSON
➔
 Document-oriented
➔
 MongoDB Anatomy
➔
 Installation
➔
 Limitations
➔
 MongoDB Admin
➔
 Basic CRUD
➔
 Indexing
                     2
NoSQL
➔
 “Not only SQL”
➔
 Schema less
➔
 Denormalize
➔
 Not join
➔
 Scaling




                  3
Know MongoDB
➔NoSQL โดยใช้แนวคิด document store
➔มี driver สำำหรับภำษำต่ำงๆ เช่น Java, PHP,
Python, Ruby, C/C++, C# .NET, Perl etc.
➔
 Javascript interface (Mongo shell)
➔
 Cross-platform Linux, Mac, Windows
➔
 Open source with Apache license
➔
 More info on http://mongodb.org



                                              4
BSON/1
➔Format แลกเปลี่ยนข้อมูลของที่ใช้ใน Mongo
➔ย่อมำจำก Binary-JSON
➔ไม่ใช่ JSON แต่คล้ำยกันเป็ น JSON-like
➔มี type พิเศษเช่น Date และ BinData ได้
➔
 Data type
  ➔
    Primitive type – int, float, string ...
  ➔
    Special type – RegEx, Function, Date
Ref: http://bsonspec.org/#/specification

                                              5
BSON/2
➔
 Primitive type
➔
 Special type
  ➔
    RegEx (PCRE) → {x : /^a/i}
  ➔
    Date → {x : new Date()}
  ➔
    Function → {x : function a(){...}}
  ➔
    Embeded document → {x : {y: 1}}
  ➔
    …etc.




                                         6
Document-Oriented
➔
 BSON
➔ใช้รูปแบบ Key และ Value เหมือน Map,


HashMap, Associative array
➔โครงสร้ำง BSON อย่ำงง่ำย


    {
        field1 : val1,
        field2 : val2,
        field3 : [v1,v2,v3]
    }

                                       7
ตัวอย่าง 1 Document
สมมุตข้อมูลของคนๆ หนึ่ งจะได้ BSON เป็ น
➔    ิ
{
    “name” : “Javier”,
    “middlename” : “Hernandez”,
    “surname” : “Balcazar”,
    “alias” : [“Chicharito”, “ถั่วน้อย”]
}


                                           8
MongoDB Anatomy/1




                    9
MongoD Anatomy/2
เทียบเคียง relational DB
➔



       MongoDB                      Relational DB
Database                    Database
Collection                  Table
Document                    Row
Key                         Field
Int, String, NULL, Object   Int, String, NULL, Object
etc                         etc

                                                        10
MongoDB Anatomy/3
ฐำนข้อมูลเริ่มต้นของ Mongodb
➔


    ➔   admin สำำหรับจัดกำร db , user ที่ถกเพิ่ม
                                          ู
    ใน db นี้ มีสทธิจัดกำร db ได้
                 ิ
    ➔   local ข้อมูลใน db จะไม่ถูก replicate (ถ้ำ
    ใช้คณสมบัตน้ี ) ใช้สำำหรับ single server
        ุ     ิ
    เท่ำนั้น



                                                    11
Installation
➔
 All OSes http://mongodb.org/downloads
➔
 Support 32, 64bits OS




                                         12
Limitations
➔“mongodb” ไม่รองรับ CPU แบบ big-endian
(Mac PPC)
➔32bits OS ใช้พ้ นที่ db ได้แค่ 2GB :(
                 ื




                                      13
MongoDB Admin
➔
 Mongo Shell
  ➔
    “mongo” command
  ➔
    Configuration
  ➔query,สร้ำง,ลบ,แก้ไข,ตั้งค่ำ db

➔
 Mongo Daemon
  ➔
    “mongod” command
  ➔
    Listen on port 27017 (default)
  ➔รันเพื่อให้ mongodb ทำำงำน




                                     14
Mongo Shell/1
kor@pluto:~$mongo
MongoDB shell version: 2.0.0
connecting to: test
>
คำาสังเมพส์สส์
     ่       ์
> help ← คำำสั่งทั่วไป
> db.help() ← คำำสั่งเกี่ยวกับกำรจัดกำรฐำน
ข้อมูล (หลังจำกเลือกฐำนข้อมูลแล้ว)

                                             15
Mongo Shell/2
เพิม user สำาหรับ admin
   ่
>use admin
>db.addUser('user','passwd')
Login ในฐานะ admin
>db.auth('user','passwd')




                               16
Mongo Daemon
➔
 kor@pluto:~$mongod
➔
 Options
  ➔
    --dbpath
  ➔
    --bind_ip
  ➔
    --port
➔ยังไม่หนำำใจ? → mongod --help โลด


➔Ctrl+C เพื่อหยุดกำรทำำงำน mongod
➔หรือmongo shell สั่ง db.shutdownServer()
(สถำนะ admin เท่ำนั้น!)
                                        17
Basic CRUD
➔
 CRUD – Create, Read, Update, Delete
➔และ Insert




                                       18
Create/1
➔Mongo shell ไม่มีคำำสั่งสร้ำง db มีแต่คำำสั่ง
สร้ำง collection
➔แล้วสร้ำงยังไง?




                                                 19
Create/2
>use dbname ← ใช้คำำสั่ง use ได้เลย
>db.createCollection('colname')
>{“ok” : 1}




                                      20
Read (Query)/1
>db.find()
>db.findOne()




                 21
Read (Query)/2
ตัวอย่างคำาสัง query เทียบกับ MySQL
              ่
SELECT * FROM table
>db.col.find()

SELECT a,b FROM table ORDER BY c DESC
>db.col.find({},{a:1,b:1}).sort({c:-1})

SELECT * FROM users WHERE a=1 or b=2
>db.col.find({$or : [{a:1}, {b:2}]})

                                          22
Read (Query)/3
SELECT * FROM a = NULL
>db.col.find({a : {$eq : null, $exists : true}})

SELECT a,b FROM table WHERE a > 10
AND b <=20
>db.col.find({a:{$gt : 10}, b : {$lte : 20}})

SELECT COUNT(*) FROM table
>db.col.find().count()

                                                   23
Read (Query)/4
SELECT * FROM a LIKE '%key'
>db.col.find({a : /key$/})

SELECT * FROM table a like '%key%'
>db.col.find({a : /key/i })

SELECT COUNT(a) FROM table
>db.col.find({a : {$exists : true}}).count()



                                               24
Read (Query)/5
Document ที่มี embeded-document
{a : 1, e : {e1 :1}}

>db.col.find({e.e1 : 1})




                                  25
Update/1
UPDATE table SET a='active' WHERE b='1'
>db.col.update({b : 1},{$set : {a : 'active'}})




                                                  26
Update/2
UPDATE table SET a='active' WHERE b='1'
>db.col.update({b : 1},{$set : {a : 'active'}})




                                                  27
Delete
DELETE FROM table
>db.col.remove()

DELETE FROM table WHERE a = 'inactive'
>db.col.delete({a : 'inactive'})

คำาเตือน! MongoDB ลบแล้ว ลบเลยไม่มี
Rollback


                                         28
Insert/1
➔
 db.collection.insert({a : 1, b : 'T', c : null, d :
true, e : {e1 : 1}})
➔MongoDB เป็ น horizontal scale เพิ่ม fields


(column) เข้ำไปได้เรื่อยๆ




                                                   29
Insert/2
Bulk-insert ข้อมูลจำำนวน 1,000,000 docs
➔


เขียน Javascript ใน Mongo shell ได้
➔




                                          30
Insert/3
>for(var i=1;i <= 1000000;i++){
...db.col.insert({id : i , text : “I am number ”
   + i , author : “user” + I})
…}

1,000,000 docs ใช้เวลำ ~1 นำที

*ทดสอบบน CPU core i5, RAM 4GB, Ubuntu 64bits



                                                   31
Indexing/1
เพิ่ม performance ในกำร query
➔


query เร็วส์ส์ส์
➔




                                32
Indexing/2
db.testdata.find({id:99999}).explain()
{
  "cursor" : "BasicCursor",
  "nscanned" : 1000000,
  "nscannedObjects" : 1000000,
  "n" : 1,
  "millis" : 1109,
  ….
}

                                         33
Indexing/3
db.testdata.find({id:99999}).explain()
{
  "cursor" : "BasicCursor",
  "nscanned" : 1000000,
  "nscannedObjects" : 1000000,
  "n" : 1,
  "millis" : 96,
  ….
}

                                         34
Indexing/4
สร้ำง index
>db.col.ensureIndex({a : 1})

ดูรำยละเอียด index
>db.system.indexes.find()

ลบ index
>db.dropIndex('a_1')

                               35
Indexing/5
➔ระวัง! สร้ำง index หลำย column เกินไปอำจ
ทำำให้ insert ช้ำลง :(




                                        36
Sharding, Replication

    ยังใช้ไม่เปน
               ็
         ตัดจบ

                        37
….



     Q&A

           38

Más contenido relacionado

Similar a Introduction to MongoDB (6)

Ch07 bind9-part2
Ch07 bind9-part2Ch07 bind9-part2
Ch07 bind9-part2
 
php5new
php5newphp5new
php5new
 
Sql theory
Sql theorySql theory
Sql theory
 
c# part1.pptx
c# part1.pptxc# part1.pptx
c# part1.pptx
 
Know1 3
Know1 3Know1 3
Know1 3
 
lesson4 JSP
lesson4 JSPlesson4 JSP
lesson4 JSP
 

Introduction to MongoDB