MongoDB Common Commands

Source: Internet
Author: User

Super User-Related:

    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. D B.printcollectionstats ()
    18. #查看主从复制状态
    19. db.printreplicationinfo ()
    20. #修复数据库
    21. Db.repairdatabase ()
    22. #设置记录profiling, 0=off 1=slow 2=all
    23. db.setprofilinglevel (1)
    24. # View Profiling
    25. Show profile
    26. #拷贝数据库
    27. db.copydatabase (' mail_addr ', ' mail_addr_tmp ')
    28. #删除collection
    29. db.mail_addr.drop ()
    30. #删除当前的数据库
    31. db.dropdatabase ()
  • Client connections
    1. /usr/local/mongodb/bin/mongo 8.8.88/ixigualib-u ixigua-p ' pwd '
  • Delete and change
    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条件修改, insert if not present, allow multiple records to be modified
    6. Db.foo.update ({' yy ': 5},{' $set ': {' xx ': 2}},upsert=true,multi=true)
    7. Records of #删除yy =5
    8. Db.foo.remove ({' yy ': 5})
    9. #删除所有的记录
    10. Db.foo.remove ()
  • index  
    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 ')
  • query  
    1. #查找所有
    2. db.foo.find ()
    3. #查找一条记录
    4. Li>db.foo.findone ()
    5. #根据条件检索10条记录
    6. db.foo.find ({' msg ': ' Hello 1 '}). Limit (Ten)
    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. #> action
    15. Db.foo.find ({"timestamp": {"$gte": 2}})
    16. #子对象的查找
    17. db.foo.find ({' address.city ': ' Beijing '})
  • Management
    1. #查看collection数据的大小
    2. Db.deliver_status.dataSize ()
    3. #查看colleciont状态
    4. Db.deliver_status.stats ()
    5. #查询所有索引的大小
    6. Db.deliver_status.totalIndexSize ()

MongoDB Common Commands

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.