欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于MySQL到MongoDB簡(jiǎn)易對(duì)照表的詳解

 更新時(shí)間:2013年06月03日 12:05:05   作者:  
本篇文章是對(duì)從MySQL到MongoDB的簡(jiǎn)易對(duì)照表進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
查詢:
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', ‘a(chǎn)ge' : 25})
如果你想在MySQL里添加一個(gè)字段,你必須:
ALTER TABLE user….
但在MongoDB里你只需要:
db.user.insert({‘name' : 'starlee', ‘a(chǎn)ge' : 25, ‘email' : 'starlee@starlee.com'})
刪除:
MySQL:
DELETE * FROM user
Mongo:
db.user.remove({})
MySQL:
DELETE FROM user WHERE age < 30
Mongo:
db.user.remove({‘a(chǎn)ge' : {$lt : 30}})
$gt : > ; $gte : >= ; $lt : < ; $lte : <= ; $ne : !=
更新:
MySQL:
UPDATE user SET `age` = 36 WHERE `name` = 'starlee'
Mongo:
db.user.update({‘name' : 'starlee'}, {$set : {‘a(chǎn)ge' : 36}})
MySQL:
UPDATE user SET `age` = `age` + 3 WHERE `name` = 'starlee'
Mongo:
db.user.update({‘name' : 'starlee'}, {$inc : {‘a(chǎn)ge' : 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({‘a(chǎn)ge' : {$in : [25, 35, 45]}})
MySQL:
SELECT * FROM user ORDER BY age DESC
Mongo:
db.user.find().sort({‘a(chǎn)ge' : -1})
MySQL:
SELECT DISTINCT(name) FROM user WHERE age > 20
Mongo:
db.user.distinct(‘name', {‘a(chǎn)ge': {$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})
發(fā)現(xiàn)很多人在搜MongoDB循環(huán)插入數(shù)據(jù),下面把MongoDB循環(huán)插入數(shù)據(jù)的方法添加在下面:
for(var i=0;i<100;i++)db.test.insert({uid:i,uname:'nosqlfan'+i});
上面一次性插入一百條數(shù)據(jù),大概結(jié)構(gòu)如下:
{ “_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″ }
簡(jiǎn)易對(duì)照表
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  對(duì)一個(gè)數(shù)字字段field增加value
(10) $set  就是相當(dāng)于sql的set field = value
(11) $unset  就是刪除字段  
(12) $push  把value追加到field里面去,field一定要是數(shù)組類型才行,如果field不存在,會(huì)新增一個(gè)數(shù)組類型加進(jìn)去
(13) $pushAll  同$push,只是一次可以追加多個(gè)值到一個(gè)數(shù)組字段內(nèi)
(14) $addToSet  增加一個(gè)值到數(shù)組內(nèi),而且只有當(dāng)這個(gè)值不在數(shù)組內(nèi)才增加。
(15) $pop  刪除最后一個(gè)值:{ $pop : { field : 1 } }刪除第一個(gè)值:{ $pop : { field : -1 } }注意,只能刪除一個(gè)值,也就是說(shuō)只能用1或-1,而不能用2或-2來(lái)刪除兩條。mongodb 1.1及以后的版本才可以用
(16) $pull  從數(shù)組field內(nèi)刪除一個(gè)等于value值
(17) $pullAll  同$pull,可以一次刪除數(shù)組內(nèi)的多個(gè)值
(18) $ 操作符  是他自己的意思,代表按條件找出的數(shù)組里面某項(xiàng)他自己。這個(gè)比較坳口,就不說(shuō)了。
二、CURD 增、改、讀、刪
增加
復(fù)制代碼 代碼如下:

db.collection->insert({'name' => 'caleng', 'email' => 'admin#admin.com'});

是不是灰常簡(jiǎn)單呀,對(duì)就是這么簡(jiǎn)單,它沒(méi)有字段的限制,你可以隨意起名,并插入數(shù)據(jù)
復(fù)制代碼 代碼如下:

db.collection.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } ); 只更新了第一條大于1記錄
db.collection.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true ); 大于3的記錄 全更新了
db.collection.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false ); 大于4的記錄 只加進(jìn)去了第一條
db.collection.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); 大于5的記錄 全加進(jìn)去

查詢
復(fù)制代碼 代碼如下:

db.collection.find(array('name' => 'bailing'), array('email'=>'email@qq.com'))
db.collection.findOne(array('name' => 'bailing'), array('email''email@qq.com'))

大家可以看到查詢我用了兩種不同的寫法,這是為什么,其實(shí)這跟做菜是一樣的,放不同的調(diào)料,炒出的菜是不同的味道。下面給大家說(shuō)一下,這兩種調(diào)料的不同作用。
findOne()只返回一個(gè)文檔對(duì)象,find()返回一個(gè)集合列表。
也就是說(shuō)比如,我們只想查某一條特定數(shù)據(jù)的詳細(xì)信息的話,我們就可以用findOne();
如果想查詢某一組信息,比如說(shuō)一個(gè)新聞列表的時(shí)候,我們就可以作用find();
那么我想大家這時(shí)一定會(huì)想到我想對(duì)這一個(gè)列表排序呢,no problem mongodb會(huì)為您全心全意服務(wù)
復(fù)制代碼 代碼如下:

db.collection.find().sort({age:1}); //按照age正序排列
db.collection.find().sort({age:-1}); //按照age倒序排列
db.collection.count(); //得到數(shù)據(jù)總數(shù)
db.collection.limit(1); //取數(shù)據(jù)的開始位置
db.collection.skip(10); //取數(shù)據(jù)的結(jié)束位置
//這樣我們就實(shí)現(xiàn)了一個(gè)取10條數(shù)據(jù),并排序的操作。

刪除
刪除有兩個(gè)操作 remove()和drop()
復(fù)制代碼 代碼如下:

db.collection.remove({"name",'jerry'}) //刪除特定數(shù)據(jù)
db.collection.drop() //刪除集合內(nèi)的所有數(shù)據(jù)

distinct操作
復(fù)制代碼 代碼如下:

db.user.distinct('name', {'age': {$lt : 20}})

2. 熟悉MongoDB的數(shù)據(jù)操作語(yǔ)句,類sql
數(shù)據(jù)庫(kù)操作語(yǔ)法
mongo --path
db.AddUser(username,password) 添加用戶
db.auth(usrename,password) 設(shè)置數(shù)據(jù)庫(kù)連接驗(yàn)證
db.cloneDataBase(fromhost) 從目標(biāo)服務(wù)器克隆一個(gè)數(shù)據(jù)庫(kù)
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 復(fù)制數(shù)據(jù)庫(kù)fromdb---源數(shù)據(jù)庫(kù)名稱,todb---目標(biāo)數(shù)據(jù)庫(kù)名稱,fromhost---源數(shù)據(jù)庫(kù)服務(wù)器地址
db.createCollection(name,{size:3333,capped:333,max:88888}) 創(chuàng)建一個(gè)數(shù)據(jù)集,相當(dāng)于一個(gè)表
db.currentOp() 取消當(dāng)前庫(kù)的當(dāng)前操作
db.dropDataBase() 刪除當(dāng)前數(shù)據(jù)庫(kù)
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一個(gè)數(shù)據(jù)集合,同用法:db['cname'] or db.cname
db.getCollenctionNames() 取得所有數(shù)據(jù)集合的名稱列表
db.getLastError() 返回最后一個(gè)錯(cuò)誤的提示消息
db.getLastErrorObj() 返回最后一個(gè)錯(cuò)誤的對(duì)象
db.getMongo() 取得當(dāng)前服務(wù)器的連接對(duì)象get the server connection object
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回當(dāng)操作數(shù)據(jù)庫(kù)的名稱
db.getPrevError() 返回上一個(gè)錯(cuò)誤對(duì)象
db.getProfilingLevel() ?什么等級(jí)
db.getReplicationInfo() ?什么信息
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(殺死)在當(dāng)前庫(kù)的當(dāng)前操作
db.printCollectionStats() 返回當(dāng)前庫(kù)的數(shù)據(jù)集狀態(tài)
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回當(dāng)前數(shù)據(jù)庫(kù)是否為共享數(shù)據(jù)庫(kù)
db.removeUser(username) 刪除用戶
db.repairDatabase() 修復(fù)當(dāng)前數(shù)據(jù)庫(kù)
db.resetError()
db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into {cmdObj:1}
db.setProfilingLevel(level) 0=off,1=slow,2=all
db.shutdownServer() 關(guān)閉當(dāng)前服務(wù)程序
db.version() 返回當(dāng)前程序的版本信息
數(shù)據(jù)集(表)操作語(yǔ)法
db.linlin.find({id:10}) 返回linlin數(shù)據(jù)集ID=10的數(shù)據(jù)集
db.linlin.find({id:10}).count() 返回linlin數(shù)據(jù)集ID=10的數(shù)據(jù)總數(shù)
db.linlin.find({id:10}).limit(2) 返回linlin數(shù)據(jù)集ID=10的數(shù)據(jù)集從第二條開始的數(shù)據(jù)集
db.linlin.find({id:10}).skip(8) 返回linlin數(shù)據(jù)集ID=10的數(shù)據(jù)集從0到第八條的數(shù)據(jù)集
db.linlin.find({id:10}).limit(2).skip(8) 返回linlin數(shù)據(jù)集ID=1=的數(shù)據(jù)集從第二條到第八條的數(shù)據(jù)
db.linlin.find({id:10}).sort() 返回linlin數(shù)據(jù)集ID=10的排序數(shù)據(jù)集
db.linlin.findOne([query]) 返回符合條件的一條數(shù)據(jù)
db.linlin.getDB() 返回此數(shù)據(jù)集所屬的數(shù)據(jù)庫(kù)名稱
db.linlin.getIndexes() 返回些數(shù)據(jù)集的索引信息
db.linlin.group({key:...,initial:...,reduce:...[,cond:...]})
db.linlin.mapReduce(mayFunction,reduceFunction,<optional params>)
db.linlin.remove(query) 在數(shù)據(jù)集中刪除一條數(shù)據(jù)
db.linlin.renameCollection(newName) 重命名些數(shù)據(jù)集名稱
db.linlin.save(obj) 往數(shù)據(jù)集中插入一條數(shù)據(jù)
db.linlin.stats() 返回此數(shù)據(jù)集的狀態(tài)
db.linlin.storageSize() 返回此數(shù)據(jù)集的存儲(chǔ)大小
db.linlin.totalIndexSize() 返回此數(shù)據(jù)集的索引文件大小
db.linlin.totalSize() 返回些數(shù)據(jù)集的總大小
db.linlin.update(query,object[,upsert_bool]) 在此數(shù)據(jù)集中更新一條數(shù)據(jù)
db.linlin.validate() 驗(yàn)證此數(shù)據(jù)集
db.linlin.getShardVersion() 返回?cái)?shù)據(jù)集共享版本號(hào)

相關(guān)文章

最新評(píng)論