NoSQL and MongoDB
1
NoSQL and MongoDB 1 2 Introduction to NoSQL Based on a - - PowerPoint PPT Presentation
NoSQL and MongoDB 1 2 Introduction to NoSQL Based on a presentation by Traversy Media 3 What is NoSQL? Not only SQL SQL means Relational model Strong typing ACID compliance Normalization NoSQL means more freedom or flexibility 4
1
2
3
Based on a presentation by Traversy Media
Relational model Strong typing ACID compliance Normalization …
4
5
6
7
8
9
ID Name Email … 1 Jack jack@example.com 2 Jill jill@example.net 3 Alex alex@example.org Document 1 { “id”: 1, “name”:”Jack”, “email”: “jack@example.com”, “address”: {“street”: “900 university ave”, “city”: “Riverside”, state: “CA”}, “friend_ids”: [3, 55, 123]} Document 2 { “id”: 2, “name”: “Jill”, “email”: “jill@example.net”, “hobbies”: [“hiking”, “cooking”]}
10
ID 1 2 3 Name Jack Jill Alex Email … … … ID Name Email … 1 Jack jack@example.com 2 Jill jill@example.net 3 Alex alex@example.org
11
1 à Jack jack@example.com … 2 à Jill jill@example.net … 3 à Alex alex@example.org … ID Name Email … 1 Jack jack@example.com 2 Jill jill@example.net 3 Alex alex@example.org
12
Database
Relation (Table) : Schema
Record (Tuple) : Data
Database
Collection : No predefined schema
Document : Schema+data
13
Document 1 { “id”: 1, “name”:”Jack”, “email”: “jack@example.com”, “address”: {“street”: “900 university ave”, “city”: “Riverside”, state: “CA”}, “friend_ids”: [3, 55, 123]}
14
15
db.users.insert({name: “Jack”, email: “jack@example.com”}); Install: Check the MongoDB website https://docs.mongodb.com/manual/installation/ db.users.find(); db.users.find({name: “Jack”}); db.users.update({name: "Jack"}, {$set: {hobby: "cooking"}}); updateOne, updateMany, replaceOne db.users.remove({name: "Alex"}); deleteOne, deleteMany
Create collection and insert a document Retrieve all/some documents Update Delete
https://docs.mongodb.com/manual/crud/
16
db.createCollection("students", { validator: { $jsonSchema: { bsonType: "object", required: [ "name", "year", "major", "address" ], properties: { name: { bsonType: "string", description: "must be a string and is required" }, … } }} }
https://docs.mongodb.com/manual/core/schema-validation/
17
mongod --wiredTigerIndexConfigString "type=lsm,block_compressor=zlib"
Override default configuration
18
https://github.com/wiredtiger/wiredtiger/wiki/Btree-vs-LSM
19
https://docs.mongodb.com/manual/indexes/
db.collection.createIndex({name: -1});
db.collection.createIndex( { name: 1, score: -1});
Creates an index entry for each value
20
https://docs.mongodb.com/manual/indexes/
Uses geohash to convert two dimensions to one dimension 2d indexes: For Euclidean spaces 2d sphere: spherical (earth) geometry Works with multikey indexes for multiple locations (e.g., pickup and dropoff locations for taxis)
Automatically removes stop words Stems the works to store the root only
21
22
In contrast, non-sparse indexes assume a null value if the index field does not exist
23
db.restaurants.createIndex( { cuisine: 1, name: 1 }, { partialFilterExpression: { rating: { $gt: 5 } } } )
Min key (internal type) Null Numbers (32-bit integer, 64-bit integer, double) Symbol, String Object Array Binary data Object ID Boolean Date, timestamp Regular expression Max key (internal type)
24
https://docs.mongodb.com/v3.6/reference/bson-type-comparison-order/
Numbers: All converted to a common type Strings
Alphabetically (default) Collation (i.e., locale and language)
Arrays
<: Smallest value of the array >: Largest value of the array Empty arrays are treated as null
Object
Compare fields in the order of appearance Compare <name,value> for each field
25
Replication (Similar to MySQL) Sharding (True horizontal scaling)
26
Replication
https://docs.mongodb.com/manual/replication/
Sharding
https://docs.mongodb.com/manual/sharding/
Log-structured Merge Tree (LSM)
27
28
29
New record Index Log
30
New record
Randomly updated disk page(s) Append a disk page
31
New records Log Log Log Log Log Flush Merge Bigger log
O’Neil, Patrick, Edward Cheng, Dieter Gawlick, and Elizabeth O’Neil. "The log-structured merge-tree (LSM-tree)." Acta Informatica 33, no. 4 (1996): 351-385.
32
20 40 60 80 100 120 1 9 9 7 1 9 9 8 1 9 9 9 2 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 1 2 1 1 2 1 2 2 1 3 2 1 4 2 1 5 2 1 6 2 1 7 2 1 8
Citations
Citations
First report from Google mentioning LSM BigTable paper
33
34