MongoDB常用操作

01

mongo –path

02

db.AddUser(username,password)  添加用戶

03

db.auth(usrename,password)     設置數據庫連接驗證

04

db.cloneDataBase(fromhost)     從目標服務器克隆一個數據庫

05

db.commandHelp(name)           returns thehelpforthe command

06

db.copyDatabase(fromdb,todb,fromhost)  複製數據庫fromdb—源數據庫名稱,todb—目標數據庫名稱,fromhost—源數據庫服務器地址

07

db.createCollection(name,{size:3333,capped:333,max:88888})  創建一個數據集,相當於一個表

08

db.currentOp()                 取消當前庫的當前操作

09

db.dropDataBase()              刪除當前數據庫

10

db.eval(func,args)             run code server-side

11

db.getCollection(cname)        取得一個數據集合,同用法:db['cname']ordb.cname

12

db.getCollenctionNames()       取得所有數據集合的名稱列表

13

db.getLastError()              返回最後一個錯誤的提示消息

14

db.getLastErrorObj()           返回最後一個錯誤的對象

15

db.getMongo()                  取得當前服務器的連接對象get the server connectionobject

16

db.getMondo().setSlaveOk()     allow this connection to readfromthen nonmaster membr of a replica pair

17

db.getName()                   返回當操作數據庫的名稱

18

db.getPrevError()              返回上一個錯誤對象

19

db.getProfilingLevel()         ?什麼等級

20

db.getReplicationInfo()        ?什麼信息

21

db.getSisterDB(name)           get the db at the same server as this onew

22

db.killOp()                    停止(殺死)在當前庫的當前操作

23

db.printCollectionStats()      返回當前庫的數據集狀態

24

db.printReplicationInfo()

25

db.printSlaveReplicationInfo()

26

db.printShardingStatus()       返回當前數據庫是否爲共享數據庫

27

db.removeUser(username)        刪除用戶

28

db.repairDatabase()            修復當前數據庫

29

db.resetError()

30

db.runCommand(cmdObj)          run a database command.ifcmdObjis a string, turns it into {cmdObj:1}

31

db.setProfilingLevel(level)   0=off,1=slow,2=all

32

db.shutdownServer()            關閉當前服務程序

33

db.version()                   返回當前程序的版本信息

  數據集(表)操作語法

01

db.linlin.find({id:10})          返回linlin數據集ID=10的數據集

02

db.linlin.find({id:10}).count()  返回linlin數據集ID=10的數據總數

03

db.linlin.find({id:10}).limit(2)返回linlin數據集ID=10的數據集從第二條開始的數據集

04

db.linlin.find({id:10}).skip(8)  返回linlin數據集ID=10的數據集從0到第八條的數據集

05

db.linlin.find({id:10}).limit(2).skip(8)  返回linlin數據集ID=1=的數據集從第二條到第八條的數據

06

db.linlin.find({id:10}).sort()   返回linlin數據集ID=10的排序數據集

07

db.linlin.findOne([query])       返回符合條件的一條數據

08

db.linlin.getDB()                返回此數據集所屬的數據庫名稱

09

db.linlin.getIndexes()           返回些數據集的索引信息

10

db.linlin.group({key:…,initial:…,reduce:…[,cond:...]})

11

db.linlin.mapReduce(mayFunction,reduceFunction,<optional params>)

12

db.linlin.remove(query)                      在數據集中刪除一條數據

13

db.linlin.renameCollection(newName)          重命名些數據集名稱

14

db.linlin.save(obj)                          往數據集中插入一條數據

15

db.linlin.stats()                            返回此數據集的狀態

16

db.linlin.storageSize()                      返回此數據集的存儲大小

17

db.linlin.totalIndexSize()                   返回此數據集的索引文件大小

18

db.linlin.totalSize()                        返回些數據集的總大小

19

db.linlin.update(query,object[,upsert_bool])在此數據集中更新一條數據

20

db.linlin.validate()                         驗證此數據集

21

db.linlin.getShardVersion()                  返回數據集共享版本號

22

db.linlin.find({‘name’:'foobar’})    select*fromlinlin where name=’foobar’

23

db.linlin.find()                     select*fromlinlin

24

db.linlin.find({‘ID’:10}).count()    select count(*)fromlinlin whereID=10

25

db.linlin.find().skip(10).limit(20)  從查詢結果的第十條開始讀20條數據  select * from linlin limit 10,20 ———-mysql

26

db.linlin.find({‘ID’:{$in:[25,35,45]}})  select * from linlin where IDin(25,35,45)

27

db.linlin.find().sort({‘ID’:-1})      select * from linlin order by IDdesc

28

db.linlin.distinct(‘name’,{‘ID’:{$lt:20}})   select distinct(name)fromlinlin whereID<20

29

db.linlin.group({key:{‘name’:true},cond:{‘name’:'foo’},reduce:function(obj,prev){prev.msum+=obj.marks;},initial:{msum:0}})

30

select name,sum(marks)fromlinlin group by name

31

db.linlin.find(‘this.ID<20′,{name:1})     select name from linlin where ID<20

32

db.linlin.insert({‘name’:'foobar’,'age’:25})  insert into linlin (‘name’,'age’)values(‘foobar’,25)

33

db.linlin.insert({‘name’:'foobar’,'age’:25,’email’:'cclove2@163.com’})

34

db.linlin.remove({})                   delete*fromlinlin

35

db.linlin.remove({‘age’:20})           delete linlin where age=20

36

db.linlin.remove({‘age’:{$lt:20}})     delete linlin where age<20

37

db.linlin.remove({‘age’:{$lte:20}})    delete linlin where age<=20

38

db.linlin.remove({‘age’:{$gt:20}})     delete linlin where age>20

39

db.linlin.remove({‘age’:{$gte:20}})    delete linlin where age>=20

40

db.linlin.remove({‘age’:{$ne:20}})     delete linlin where age!=20

41

db.linlin.update({‘name’:'foobar’},{$set:{‘age’:36}})  update linlinsetage=36where name=’foobar’

42

db.linlin.update({‘name’:'foobar’},{$inc:{‘age’:3}})   update linlinsetage=age+3where name=’foobar’


超級用戶相關:

  1. use admin
  2. #增加或修改用戶密碼
  3. db.addUser(ixigua,'pwd')
  4. #查看用戶列表
  5. db.system.users.find()
  6. #用戶認證
  7. db.auth(ixigua,'pwd')
  8. #刪除用戶
  9. db.removeUser('mongodb')
  10. #查看所有用戶
  11. show users
  12. #查看所有數據庫
  13. show dbs
  14. #查看所有的collection
  15. show collections
  16. #查看各collection的狀態
  17. db.printCollectionStats()
  18. #查看主從複製狀態
  19. db.printReplicationInfo()
  20. #修復數據庫
  21. db.repairDatabase()
  22. #設置記錄profiling,0=off 1=slow 2=all
  23. db.setProfilingLevel(1)
  24. #查看profiling
  25. show profile
  26. #拷貝數據庫
  27. db.copyDatabase('mail_addr','mail_addr_tmp')
  28. #刪除collection
  29. db.mail_addr.drop()
  30. #刪除當前的數據庫
  31. db.dropDatabase()
客戶端連接
  1. /usr/local/mongodb/bin/mongo 8.8.88/ixigualib -u ixigua -p 'pwd'
增刪改
  1. #存儲嵌套的對象
  2. db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})
  3. #存儲數組對象
  4. db.user_addr.save({'Uid':'[email protected]','Al':['[email protected]','[email protected]']})
  5. #根據query條件修改,如果不存在則插入,允許修改多條記錄
  6. db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true)
  7. #刪除yy=5的記錄
  8. db.foo.remove({'yy':5})
  9. #刪除所有的記錄
  10. db.foo.remove()
索引
  1. #增加索引:1(ascending),-1(descending)
  2. db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
  3. #索引子對象
  4. db.user_addr.ensureIndex({'Al.Em': 1})
  5. #查看索引信息
  6. db.deliver_status.getIndexes()
  7. db.deliver_status.getIndexKeys()
  8. #根據索引名刪除索引
  9. db.user_addr.dropIndex('Al.Em_1')
查詢
  1. #查找所有
  2. db.foo.find()
  3. #查找一條記錄
  4. db.foo.findOne()
  5. #根據條件檢索10條記錄
  6. db.foo.find({'msg':'Hello 1'}).limit(10)
  7. #sort排序
  8. db.deliver_status.find({'From':'[email protected]'}).sort({'Dt',-1})
  9. db.deliver_status.find().sort({'Ct':-1}).limit(1)
  10. #count操作
  11. db.user_addr.count()
  12. #distinct操作
  13. db.foo.distinct('msg')
  14. #>操作
  15. db.foo.find({"timestamp": {"$gte" : 2}})
  16. #子對象的查找
  17. db.foo.find({'address.city':'beijing'})
管理
  1. #查看collection數據的大小
  2. db.deliver_status.dataSize()
  3. #查看colleciont狀態
  4. db.deliver_status.stats()
  5. #查詢所有索引的大小
  6. db.deliver_status.totalIndexSize()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章