请使用firefox,chrome等最新浏览器浏览本站。

mogodb与mysql基本操作对比

后端 阿豹 479次浏览 0个评论 扫描二维码

查询:
MySQL:
SELECT * FROM user
Mongo:
use user
show tables;
db.表名.find()

MySQL:
SELECT * FROM user WHERE name = ‘starlee’
Mongo:
db.user.find({‘name’ : ‘starlee’})

mongodb存在某一字段
db.user.find({name:{$exists:1}})

MySQL:
select name, age, skills from users where name = ‘hurry’ or age = 18;
Mongo:
db.users.find({ ‘$or’ : [{‘name’ : ‘hurry’}, {‘age’ : 18}] },{‘name’ : 1, ‘age’ : 1, ‘skills’ : 1});

插入:
MySQL:
INSERT INOT user (`name`, `age`) values (‘starlee’,25);
Mongo:
db.user.insert({‘name’ : ‘starlee’, ‘age’ : 25})

删除:
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 : !=  $set : =
$inc : +

更新:
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}})

==============Split=======================

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})


喜欢 (0)or分享 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到