基于MySQL到MongoDB简易对照表的详解

查询:
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})
发现很多人在搜MongoDB循环插入数据,下面把MongoDB循环插入数据的方法添加在下面:
for(var i=0;i<100;i++)db.test.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″ }
简易对照表
SQL Statement                                                  Mongo Query Language Statement
CREATE TABLE USERS (a Number, b Number)         implicit; can be done explicitly
INSERT INTO USERS VALUES(1,1)                             db.users.insert({a:1,b:1})
SELECT a,b FROM users                                           db.users.find({}, {a:1,b:1})
SELECT * FROM users                                              db.users.find()
SELECT * FROM users WHERE age=33                      db.users.find({age:33})
SELECT a,b FROM users WHERE age=33                   db.users.find({age:33}, {a:1,b:1})
SELECT * FROM users WHERE age=33 ORDER BY name                db.users.find({age:33}).sort({name:1})
SELECT * FROM users WHERE age>33                     db.users.find({'age':{$gt:33}})})
SELECT * FROM users WHERE age<33                     db.users.find({'age':{$lt:33}})})
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 WHERE age>33 AND age<=40                                   db.users.find({'age':{$gt:33,$lte:40}})})
SELECT * FROM users ORDER BY name DESC                                   db.users.find().sort({name:-1})
CREATE INDEX myindexname ON users(name)                                   db.users.ensureIndex({name:1})
CREATE INDEX myindexname ON users(name,ts DESC)                                   db.users.ensureIndex({name:1,ts:-1})
SELECT * FROM users WHERE a=1 and b='q'                                   db.users.find({a:1,b:'q'})
SELECT * FROM users LIMIT 10 SKIP 20                                   db.users.find().limit(10).skip(20)
SELECT * FROM users WHERE a=1 or b=2                          db.users.find( { $or : [ { a : 1 } , { b : 2 } ] } )
SELECT * FROM users LIMIT 1                                          db.users.findOne()
EXPLAIN SELECT * FROM users WHERE z=3                                   db.users.find({z:3}).explain()
SELECT DISTINCT last_name FROM users                                   db.users.distinct('last_name')
SELECT COUNT(*y) FROM users                                            db.users.count()
SELECT COUNT(*y) FROM users where AGE > 30                             db.users.find({age: {'$gt': 30}}).count()
SELECT COUNT(AGE) from users                                       db.users.find({age: {'$exists': true}}).count()
UPDATE users SET a=1 WHERE b='q'                                   db.users.update({b:'q'}, {$set:{a:1}}, false, true)
UPDATE users SET a=a+2 WHERE b='q'                                   db.users.update({b:'q'}, {$inc:{a:2}}, false, true)
DELETE FROM users WHERE z="abc"                                    db.users.remove({z:'abc'});
###################################################
一、操作符
操作符相信大家肯定都知道了,就是等于、大于、小于、不等于、大于等于、小于等于,但是在mongodb里不能直接使用这些操作符。在mongodb里的操作符是这样表示的:
(1) $gt > (大于)   
(2) $lt  < (小于)   
(3) $gte  >= (大于等于)
(4) $lt  <= (小于等于)  
(5) $ne  != (不等于) 
(6) $in  in (包含)      
(7) $nin  not in (不包含)  
(8) $exists  exist (字段是否存在) 
(9) $inc  对一个数字字段field增加value
(10) $set  就是相当于sql的set field = value
(11) $unset  就是删除字段  
(12) $push  把value追加到field里面去,field一定要是数组类型才行,如果field不存在,会新增一个数组类型加进去
(13) $pushAll  同$push,只是一次可以追加多个值到一个数组字段内
(14) $addToSet  增加一个值到数组内,而且只有当这个值不在数组内才增加。
(15) $pop  删除最后一个值:{ $pop : { field : 1 } }删除第一个值:{ $pop : { field : -1 } }注意,只能删除一个值,也就是说只能用1或-1,而不能用2或-2来删除两条。mongodb 1.1及以后的版本才可以用
(16) $pull  从数组field内删除一个等于value值
(17) $pullAll  同$pull,可以一次删除数组内的多个值
(18) $ 操作符  是他自己的意思,代表按条件找出的数组里面某项他自己。这个比较坳口,就不说了。
二、CURD 增、改、读、删
增加

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wywwyp.html