mongoDB应用(sql语句)

查询:
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})


?

if you want add a column `email` on MySQL,you must :

ALTER TABLE user….

But in Mongo,you can just:

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

?

==============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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值