資料庫操作文法
mongo --path
db.AddUser(username,password) 添加使用者
db.auth(usrename,password) 設定資料庫連接驗證
db.cloneDataBase(fromhost) 從目標伺服器複製一個資料庫
db.commandHelp(name) returns the help for the command
db.copyDatabase(fromdb,todb,fromhost) 複製資料庫fromdb---來源資料庫名稱,todb---目標資料庫名稱,fromhost---來源資料庫伺服器位址
db.createCollection(name,{size:3333,capped:333,max:88888}) 建立一個資料集,相當於一個表
db.currentOp() 取消當前庫的當前操作
db.dropDataBase() 刪除當前資料庫
db.eval(func,args) run code server-side
db.getCollection(cname) 取得一個資料集合,同用法:db['cname'] or db.cname
db.getCollenctionNames() 取得所有資料集合的名稱列表
db.getLastError() 返回最後一個錯誤的提示訊息
db.getLastErrorObj() 返回最後一個錯誤的對象
db.getMongo() 取得當前伺服器的連線物件get the server connection object
db.getMondo().setSlaveOk() allow this connection to read from then nonmaster membr of a replica pair
db.getName() 返回當操作資料庫的名稱
db.getPrevError() 返回上一個錯誤對象
db.getProfilingLevel() ?什麼等級
db.getReplicationInfo() ?什麼資訊
db.getSisterDB(name) get the db at the same server as this onew
db.killOp() 停止(殺死)在當前庫的當前操作
db.printCollectionStats() 返回當前庫的資料集狀態
db.printReplicationInfo()
db.printSlaveReplicationInfo()
db.printShardingStatus() 返回當前資料庫是否為共用資料庫
db.removeUser(username) 刪除使用者
db.repairDatabase() 修複當前資料庫
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() 關閉當前服務程式
db.version() 返回當前程式的版本資訊
db.linlin.find({'name':'foobar'}) select * from linlin where name='foobar'
db.linlin.find() select * from linlin
db.linlin.find({'ID':10}).count() select count(*) from linlin where ID=10
db.linlin.find().skip(10).limit(20) 從查詢結果的第十條開始讀20條資料 select * from linlin limit 10,20 ----------mysql
db.linlin.find({'ID':{$in:[25,35,45]}}) select * from linlin where ID in (25,35,45)
db.linlin.find().sort({'ID':-1}) select * from linlin order by ID desc
db.linlin.distinct('name',{'ID':{$lt:20}}) select distinct(name) from linlin where ID<20
db.linlin.group({key:{'name':true},cond:{'name':'foo'},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})
select name,sum(marks) from linlin group by name
db.linlin.find('this.ID<20',{name:1}) select name from linlin where ID<20
db.linlin.insert({'name':'foobar','age':25}) insert into linlin ('name','age') values('foobar',25)
db.linlin.insert({'name':'foobar','age':25,'email':'cclove2@163.com'})
db.linlin.remove({}) delete * from linlin
db.linlin.remove({'age':20}) delete linlin where age=20
db.linlin.remove({'age':{$lt:20}}) delete linlin where age<20
db.linlin.remove({'age':{$lte:20}}) delete linlin where age<=20
db.linlin.remove({'age':{$gt:20}}) delete linlin where age>20
db.linlin.remove({'age':{$gte:20}}) delete linlin where age>=20
db.linlin.remove({'age':{$ne:20}}) delete linlin where age!=20
db.linlin.update({'name':'foobar'},{$set:{'age':36}}) update linlin set age=36 where name='foobar'
db.linlin.update({'name':'foobar'},{$inc:{'age':3}}) update linlin set age=age+3 where name='foobar'
官方提供的動作陳述式對照表:
上行:SQL 動作陳述式
下行:Mongo 動作陳述式
CREATE TABLE USERS (a Number, b Number)
db.createCollection("mycoll")
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})
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()
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()
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})
EXPLAIN SELECT * FROM users WHERE z=3
db.users.find({z:3}).explain()
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'});