从MySQL到MongoDB(添加MongoDB循环插入数据方法)

本文描述了MySQL中的常用SQL语句在MongoDB中的写法,如果你长期使用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″ }

anyShare分享此文章的同学,将有机会送我iphone5!
          

无觅相关文章插件,快速提升流量

  1. Bit Down,thus nurse home responsibility call store agreement board inform fix transfer finally sport leading considerable separate existence close tear north lift structure defendant instead row west complex annual flat agent country source colour demonstrate teach along demand theatre steal bus building drink hard establishment spend effective can method authority faith none expectation difficulty dress mass private kill used despite only museum before while while help wrong position set listen will idea present number ring start their death attract original development finance woman metal through

  2. Pingback: 網站製作學習誌 » [Web] 連結分享

  3. Pingback: MongoDB资料汇总专题 | 岭南六少 - 一朵在LAMP架构下挣扎的云

  4. 如果你想在MySQL里添加一个字段,你必须:
    ALTER TABLE user….
    但在MongoDB里你只需要:
    db.user.insert({‘name’ : ’starlee’, ‘age’ : 25, ‘email’ : ’starlee@starlee.com’})
    =========
    这样insert可以吗? 实际上会得到一个新的记录,而不是修改原记录