Mongodb 和 MySql native json 对比
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});