MongoDB常用命令

來源:互聯網
上載者:User

標籤:

超級使用者相關:

 
  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()

MongoDB常用命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.