MongoDB语法 MySql语法 db.test.find({'name':'foobar'}) <==> select * from test where name='foobar' db.test.find() <==> select * from test db.test.find({'ID':10}).count() <==> select count(*) from test where ID=10 db.test.find().skip(10).limit(20) <==> select * from test limit 10,20 db.test.find({'ID':{$in:[25,35,45]}}) <==> select * from test where ID in (25,35,45) db.test.find().sort({'ID':-1}) <==> select * from test order by ID desc db.test.distinct('name',{'ID':{$lt:20}}) <==> select distinct(name) from test where ID<20 db.test.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}}) <==> select name,sum(marks) from test group by name db.test.find('this.ID<20',{name:1}) <==> select name from test where ID<20 db.test.insert({'name':'foobar','age':25})<==>insert into test ('name','age') values('foobar',25) db.test.remove({}) <==> delete * from test db.test.remove({'age':20}) <==> delete test where age=20 db.test.remove({'age':{$lt:20}}) <==> elete test where age<20 db.test.remove({'age':{$lte:20}}) <==> delete test where age<=20 db.test.remove({'age':{$gt:20}}) <==> delete test where age>20 db.test.remove({'age':{$gte:20}}) <==> delete test where age>=20 db.test.remove({'age':{$ne:20}}) <==> delete test where age!=20 db.test.update({'name':'foobar'},{$set:{'age':36}}) <==> update test set age=36 where name='foobar' db.test.update({'name':'foobar'},{$inc:{'age':3}}) <==> update test set age=age+3 where name='foobar'
1、完整语法对比
查询:
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}})
MongoDB中的大小比较符号:
$gt : >
$gte : >=
$lt : <
$lte : <=
$ne : !=
UPDATE更新:
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}})
count求总数:
MySQL:
SELECT COUNT(*) FROM user WHERE `name` = ’starlee’
Mongo:
db.user.find({‘name’ : ’starlee’}).count()
也可以写成这样:
db.user.count({‘name’:’starlee’})
LIMIT子句:
MySQL:
SELECT * FROM user limit 10,20
Mongo:
db.user.find().skip(10).limit(20)
IN子句:
MySQL:
SELECT * FROM user WHERE `age` IN (25, 35,45)
Mongo:
db.user.find({‘age’ : {$in : [25, 35, 45]}})
Order排序:(1表示正序,-1逆序)
MySQL:
SELECT * FROM user ORDER BY age DESC
Mongo:
db.user.find().sort({‘age’ : -1})
Distinct排重:
MySQL:
SELECT DISTINCT(name) FROM user WHERE age > 20
Mongo:
db.user.distinct(‘name’, {‘age’: {$lt : 20}})
Group 分组:
MySQL:
SELECT name, sum(marks) FROM user where name=’foo’ GROUP BY name
Mongo:
db.user.group({
key : {‘name’ : true},
cond: {‘name’ : ‘foo’},
reduce: function(obj,prev) { prev.msum += obj.marks; },
initial: {msum : 0}
});
获取age字段小于20的数据的name字段
MySQL:
SELECT name FROM user WHERE age < 20
Mongo:
db.user.find(‘this.age < 20′, {name : 1})
循环插入数据:
for(var i=0;i<100;i++)db.user.insert({uid:i,uname:’nosqlfan’+i});
这样一次性插入了一百条数据
{ “_id” : ObjectId(“4c876e519e86023a30dde6b8″), “uid” : 55, “uname” : “nosqlfan55″ }
{ “_id” : ObjectId(“4c876e519e86023a30dde6b9″), “uid” : 56, “uname” : “nosqlfan56″ }
{ “_id” : ObjectId(“4c876e519e86023a30dde6ba”), “uid” : 57, “uname” : “nosqlfan57″ }
{ “_id” : ObjectId(“4c876e519e86023a30dde6bb”), “uid” : 58, “uname” : “nosqlfan58″ }
{ “_id” : ObjectId(“4c876e519e86023a30dde6bc”), “uid” : 59, “uname” : “nosqlfan59″ }
{ “_id” : ObjectId(“4c876e519e86023a30dde6bd”), “uid” : 60, “uname” : “nosqlfan60″ }
来源:http://hi.baidu.com/phpha_com/item/e2fc9c03c81d15d41ff046a4