1,Mongodb 屬於NoSql中的文檔型數據庫,支持JSON存儲;Mysql從5.7開始,也支持JSON存儲,兩者有什么區別?
參考文章:
-
Which is the better way to store user related data in database using - JSON or column-per-field?
-
Using MongoDB vs MySQL with lots of JSON fields? (可以重點閱讀這篇文章)
-
Native JSON support in MYSQL 5.7 : what are the pros and cons of JSON data type in MYSQL?
-
Storing JSON in database vs. having a new column for each key
1.1 MySQL 特點:
– 屬於關系型數據庫;
-
擅長處理一列對應一個值;(For relational databases: use one column per value. Putting a JSON blob in a column makes it virtually impossible to query (and painfully slow when you actually find a query that works).)
-
創建的索引,會充分利用該列的數據類型,適用於規整的數據結構。(Relational databases take advantage of data types when indexing, and are intended to be implemented with a normalized structure.)
-
存儲JSON的場景,是該JSON數據只是查詢,或者展示使用。(If you're adding true metadata, or if your JSON is describing information that does not need to be queried and is only used for display, it may be overkill to create a separate column for all of the data points.)
-
如果不采用JSON存儲,需要三張table表示關聯關系
-
支持JSON索引:Mysql 官方文檔:The JSON Data Type
-
如下代碼,需要掃描全表(table-scan)
-
The claim about "efficient access" is misleading. It means that after the query examines a row with a JSON document, it can extract a field without having to parse the text of the JSON syntax.But it still takes a table-scan to search for rows. In other words, the query must examine every row.
SELECT * FROM t1
WHERE JSON_EXTRACT(data,"$.series") IN ...
1.2 Mongodb 特點:
-
結構靈活存儲(Schemaless);
-
將所有相關的關系存儲於一個collection;
-
支持豐富的JSON查詢(The power behind MongoDB is in the rich queries against schemaless storage.);
-
支持事務:Transactions
-
引用自"NoSql Distilled":
Advocates of schemalessness rejoice in this freedom and flexibility.With a schema, you have to figure out in advance what you need to store,
but that can be hard to do.Without a schema binding you, you can easily store whatever you need.This allows you to easily change your data storage
as you learn more about your project. You can easily add new things as you discover them. Furthermore, if you find you don’t need some things anymore,
you can just stop storing them, without worrying about losing old data as you would if you delete columns in a relational schema.
1.3 Mongodb 名詞與Oracle對應關系:
2,Mongodb 的如何監控,備份,恢復?
2.1 備份機制文檔:MongoDB Backup Methods
2.2 Mongodb 導出工具:mongodump
2.3 Mongodb 導入工具:mongorestore
2.4 Mongodb 監控文檔:monitoring for Mongodb
3,Mongodb 的簡單使用示例:
db.unicorns.insert({name: 'Horny',
dob: new Date(1992,2,13,7,47),
loves: ['carrot','papaya'],
weight: 600,
gender: 'm',
vampires: 63});
db.unicorns.insert({name: 'Aurora',
dob: new Date(1991, 0, 24, 13, 0),
loves: ['carrot', 'grape'],
weight: 450,
gender: 'f',
vampires: 43});
db.unicorns.insert({name: 'Unicrom',
dob: new Date(1973, 1, 9, 22, 10),
loves: ['energon', 'redbull'],
weight: 984,
gender: 'm',
vampires: 182});
db.unicorns.insert({name: 'Roooooodles',
dob: new Date(1979, 7, 18, 18, 44),
loves: ['apple'],
weight: 575,
gender: 'm',
vampires: 99});
db.unicorns.insert({name: 'Solnara',
dob: new Date(1985, 6, 4, 2, 1),
loves:['apple', 'carrot', 'chocolate'],
weight:550,
gender:'f',
vampires:80});
db.unicorns.insert({name:'Ayna',dob: new Date(1998, 2, 7, 8, 30),
loves: ['strawberry', 'lemon'],
weight: 733,
gender: 'f',
vampires: 40});
db.unicorns.insert({name:'Kenny',
dob: new Date(1997, 6, 1, 10, 42),
loves: ['grape', 'lemon'],
weight: 690,
gender: 'm',
vampires: 39});
db.unicorns.insert({name: 'Raleigh',
dob: new Date(2005, 4, 3, 0, 57),
loves: ['apple', 'sugar'],
weight: 421,
gender: 'm',
vampires: 2});
db.unicorns.insert({name: 'Leia',
dob: new Date(2001, 9, 8, 14, 53),
loves: ['apple', 'watermelon'],
weight: 601,
gender: 'f',
vampires: 33});
db.unicorns.insert({name: 'Pilot',
dob: new Date(1997, 2, 1, 5, 3),
loves: ['apple', 'watermelon'],
weight: 650,
gender: 'm',
vampires: 54});
db.unicorns.insert({name: 'Nimue',
dob: new Date(1999, 11, 20, 16, 15),
loves: ['grape', 'carrot'],
weight: 540,
gender: 'f'});
db.unicorns.insert({name: 'Dunx',
dob: new Date(1976, 6, 18, 18, 18),
loves: ['grape', 'watermelon'],
weight: 704,
gender: 'm',
vampires: 165});
3.1 查找:
- 關鍵詞: $lt, $lte, $gt, $gte, $ne
- 查找男性,體重大於700:
db.unicorns.find({gender: 'm',weight: {$gt: 700}})
- 查找不存在某一屬性的文檔:
db.unicorns.find({vampires:{$exists: false}})
- 查找喜歡蘋果和橘子的人:
db.unicorns.find({loves:'apple','orange']}})
- 多個或條件:
db.unicorns.find({gender: 'f', $or:[{loves: 'apple'},{weight: {$lt: 500}}]})
- 只獲取name信息, 排除_id 信息:
db.unicorns.find({}, {name: 1, _id: 0});
- 排序,按名稱降序,vampires升序
db.unicorns.find().sort({name: 1,
vampires: -1})
- 分頁查詢
db.unicorns.find()
.sort({weight: -1})
.limit(2)
.skip(1)
- 聚合查詢
- 男性和女性分別有多少人
db.unicorns.aggregate([{$group:{_id:'$gender',
total: {$sum:1}}}])
db.unicorns.aggregate([{$match: {weight:{$lt:600}}},
{$group: {_id:'$gender', total:{$sum:1},
avgVamp:{$avg:'$vampires'}}},
{$sort:{avgVamp:-1}} ])
- 索引:
db.unicorns.ensureIndex({name: 1});
// 查看執行計划
db.unicorns.find({name: 'Pilot'}).explain()
3.2 更新:
-
錯誤的更新,即替換整體文檔:
db.unicorns.update({name: 'Roooooodles'},{weight: 590})
-
更新文檔部分字段,正確操作:
$set
db.unicorns.update({name: 'Roooooodles'},
{$set: {weight: 590}})
- 數字加減:
db.unicorns.update({name: 'Pilot'},
{$inc: {vampires: -2}})
- 數組添加元素:
db.unicorns.update({name: 'Aurora'},
{$push: {loves: 'sugar'}})
- Upserts: 數據存在,即更新;不存在,則新增
db.hits.update({page: 'unicorns'},
{$inc: {hits: 1}}, {upsert:true});
- 批量更新
db.unicorns.update({},
{$set: {vaccinated: true }},
{multi:true});