3. NoSQL
➔
“Not only SQL”
➔
Schema less
➔
Denormalize
➔
Not join
➔
Scaling
3
4. 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
5. 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
6. 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
10. 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
11. MongoDB Anatomy/3
ฐำนข้อมูลเริ่มต้นของ Mongodb
➔
➔ admin สำำหรับจัดกำร db , user ที่ถกเพิ่ม
ู
ใน db นี้ มีสทธิจัดกำร db ได้
ิ
➔ local ข้อมูลใน db จะไม่ถูก replicate (ถ้ำ
ใช้คณสมบัตน้ี ) ใช้สำำหรับ single server
ุ ิ
เท่ำนั้น
11
22. 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
23. 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
24. 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