zoukankan      html  css  js  c++  java
  • MongoDB常用命令

    超级用户相关:

     
    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':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']})
      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':'ixigua@sina.com'}).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()
    作者:不得闲
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原
    文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    04Windows频繁打开和关闭端口可能引发的问题 | 07.杂项
    04WebFinger的利用 | 02.技术预研 | Social
    Hunch:自动问答和决策机
    03PubSubHubbub 和 twisted 的 Persistent connections 能力 | 07.杂项 | Python
    01获取 Twitter User Profile 的三条路径 | 07.杂项
    大中华之事件监测
    一个如此简单的杀手级应用
    07爬虫的多线程调度 | 01.数据抓取 | Python
    02Twisted 构建 Web Server 的 Socket 长链接问题 | 07.杂项 | Python
    关于Cutt.com关于Topic Engine
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/4283601.html
Copyright © 2011-2022 走看看