作者:难耐五更寒 | 来源:互联网 | 2014-05-28 16:53
查询:MySQL:SELECT*FROMuserMongo:db.user.find()MySQL:SELECT*FROMuserWHEREname’starlee’Mongo:db.user.find({‘name’:’starlee’})插入:MySQL:INSERTINOTuser(`name`,`age`)va
查询:
MySQL:
SELECT * FROM user
Mongo:
db.user.find()
MySQL:
SELECT * FROM user WHERE name = ’starlee’
Mongo:
db.user.find({‘name’ : ’starlee’})
插入:
MySQL:
INSERT INOT user (`name`, `age`) values (’starlee’,25)
Mongo:
db.user.insert({‘name’ : ’starlee’, ‘age’ : 25})
如果你想在MySQL里添加一个字段,你必须:
ALTER TABLE user….
但在MongoDB里你只需要:
db.user.insert({‘name’ : ’starlee’, ‘age’ : 25, ‘email’ :
’starlee@starlee.com’})
删除:
MySQL:
DELETE * FROM user
Mongo:
db.user.remove({})
MySQL:
DELETE FROM user WHERE age <30
Mongo:
db.user.remove({‘age’ : {$lt : 30}})
$gt : > ; $gte : >= ; $lt : <; $lte : <= ; $ne :
!=
更新:
MySQL:
UPDATE user SET `age` = 36 WHERE `name` = ’starlee’
Mongo:
db.user.update({‘name’ : ’starlee’}, {$set : {‘age’ : 36}})
MySQL:
UPDATE user SET `age` = `age` + 3 WHERE `name` = ’starlee’
Mongo:
db.user.update({‘name’ : ’starlee’}, {$inc : {‘age’ : 3}})
MySQL:
SELECT COUNT(*) FROM user WHERE `name` = ’starlee’
Mongo:
db.user.find({‘name’ : ’starlee’}).count()
MySQL:
SELECT * FROM user limit 10,20
Mongo:
db.user.find().skip(10).limit(20)
MySQL:
SELECT * FROM user WHERE `age` IN (25, 35,45)
Mongo:
db.user.find({‘age’ : {$in : [25, 35, 45]}})
MySQL:
SELECT * FROM user ORDER BY age DESC
Mongo:
db.user.find().sort({‘age’ : -1})
MySQL:
SELECT DISTINCT(name) FROM user WHERE age > 20
Mongo:
db.user.distinct(‘name’, {‘age’: {$lt : 20}})
MySQL:
SELECT name, sum(marks) FROM user GROUP BY name
Mongo:
db.user.group({
key : {‘name’ : true},
cond: {‘name’ : ‘foo’},
reduce: function(obj,prev) { prev.msum += obj.marks; },
initial: {msum : 0}
});
MySQL:
SELECT name FROM user WHERE age <20
Mongo:
db.user.find(‘this.age <20′, {name : 1})
#昨天做了个mysql(jdbc) 和 mongodb(java driver) 插入10 0000条数据的对比
mysql 一次insert一条 共insert10万次 用时 1817487ms
mysql 一次批量插入 执行一次insert 用时 2358ms
mongodb 一次insert一条 共insert10万次 用时 5489ms
mongodb从表面看是insert10万次,但真正去写数据库的时候可能只用了1到3次 这和mysql的第二种方式差不多
由此来看mongodb的插入同等情况下应该比mysql快