SQL与MongoDB的详细对比

文章通过对比MySQL和MongoDB的数据库结构、查询语法,展示了MongoDB的BSON文档模型和JSON查询方式,强调了MongoDB在查询和数据操作上的灵活性,同时指出这种灵活性可能带来的学习成本问题。
摘要由CSDN通过智能技术生成

为了更好的理解MongoDB与传统关系数据库的区别,我们将几款典型的关系数据库与MongoDB进行对比,如下图所示:

MySQLMongoDB
databasedatabase
tablecollection
indexindex
rowBSON document
columnBSON field
joinEmbedding and linking
aggregation e.g. group byaggregation pipeline
primary key Specify any unique column or column combination as primary key.primary key.In MongoDB, the primary key is automatically set to the _id field |

MongoDB查询被表示成JSON(或BSON)对象,在MongoDB中查询语句(以及其他东西,比如索引健模式)被表示成JSON(BSON),然而实际动作(比如"find")是在某种常用编程语言中实现的。下图对比了在SQL和MongoDB中的查询语句:

SQL语句MongoDB语句
create table users (a number,b number)Implicit; can also be done explicitly with  
db.createCollection("users")
Insert into users values(1,2)db.users.insert({a:1,b:2})
select a,b from usersdb.users.find({ },{a:1,b:1})
select * from usersdb.users.find()
select * from users where age=30db.users.find({age:30})
select a,b from users where age=30db.users.find({age:30},{a:1,b:1})
Select * from users where age=30 order by namedb.users.find({age:30}).sort()
select * from users where age>30db.users.find({age:{$gt:30}})
select * from users where age>=30db.users.find({age:{$gte:30}})
select * from users where age<30db.users.find({age:{$lt:30}})
select * from users where age<=30db.users.find({age:{$lte:30}})
select * from users where age!=30db.users.find({age:{$ne:30}})
select * from users where age> 18 and age <25db.users.find({age:{$gt:18,$lt:25}})
select * from users where a=18 or b=25db.users.find({$or :[{a:18} , {b:25} ] } )
select * from users where name like "%Joe%"db.users.find({name:/Joe/})
select * from users where name like "Joe%"db.users.find({name:/^Joe/})
select * from users order by name descdb.users.find().sort({name:-1}) 按降序排序
select * from users order by namedb.users.find().sort({name:1})  按升序排序
select * from users where a=2 and b='dsf'db.users.find({a:2,b:'dsf'})
select * from users limit 10 skip 15db.users.find().limit(10).skip(15)
select * from users limit 1db.users.findOne()
select order_id from orders o,order_line_items li
where li.order_id=o.order_id and li.sku=123456db.orders.find({"items.sku":123456},{_id:1})
select distinct first_name from usersdb.users.distinct("first_name")
select count(*) from usersdb.users.count()
select count(*) from users where age > 36db.users.find({age:{'$gt':36}}).count()
select count(age) from usersdb.users.find({age:{'$exists':true}}).count()
create index indexname on users(name)db.users.ensureIndex({name:1})
create index indexname on users(name,add desc)db.users.ensureIndex({name:1,add:-1})
explain select * from users where b=3db.users.find({b:3}).explain()
update users set a=1 where b='dsf'db.users.update({b:'dsf'},{$set:{a:1}})
update users set a=a+2 where b='dsf'db.users.update({b:'dsf'},{$inc:{a:2}},false,true)
delete from users where z="abc"db.users.remove({z:'abc'})
下面的例子表示的是JavaScript并且它们可以在mongo shell中执行,下面两条修改数据库文档的语句结果是相同的:

//查看原先的数据库内容

> db.chenfeng.find().forEach(printjson)

{ "_id" : ObjectId("56ad86df4486a08016f82bfa"), "a" : 1, "b" : 2 }

{ "_id" : ObjectId("56ad86f74486a08016f82bfb"), "x" : 3, "y" : 4 }

>

//将b的值改为5

> var thing=db.chenfeng.findOne({a:1})

> thing

{ "_id" : ObjectId("56ad86df4486a08016f82bfa"), "a" : 1, "b" : 2 }

> thing.b=5

5

> db.chenfeng.save(thing)

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.chenfeng.find().forEach(printjson)

{ "_id" : ObjectId("56ad86df4486a08016f82bfa"), "a" : 1, "b" : 5 }

{ "_id" : ObjectId("56ad86f74486a08016f82bfb"), "x" : 3, "y" : 4 }

>

//将b的值改回成1

> db.chenfeng.update({a:1},{$set:{b:1}},false,true)

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.chenfeng.find().forEach(printjson)

{ "_id" : ObjectId("56ad86df4486a08016f82bfa"), "a" : 1, "b" : 1 }

{ "_id" : ObjectId("56ad86f74486a08016f82bfb"), "x" : 3, "y" : 4 }

>

由这个例子可以看出,MongoDB为数据库操作提供了大量灵活的方式。当然,在增加灵活性的同时,也增加了程序员记忆的难度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值