NoSQL学习二:MongoDB基本管理命令

一、MongoDB命令帮助

  在安装MongoDB后,启动服务器进程(mongod),可以通过在客户端命令mongo实现对MongoDB的管理和监控:

这是MongoDB最上层的命令列表,主要是管理数据库的:数据库操作帮助、集合操作帮助、管理帮助。如果想了解数据库操作更详细的命令,可以直接使用db.help(),如下所示:

二、基本命令

  1、show dbs
    显示当前数据库服务器上的数据库
  2、use pagedb
     切换到指定数据库pagedb的上下文,可以在此上下文中管理pagedb数据库以及其中的集合等
  3、show collections
    显示数据库中所有的集合(collection)
  4、db.serverStatus()  
黄色别墅
    查看数据库服务器的状态。 
 
  5、查询指定数据库统计信息
    use myDatabase
    db.stats() 
 
  6、查询指定数据库包含的集合名称列表 
    
 

三、基本DDL和DML

  3.1 数据库操作

  •   切换、创建数据库 use yourDB
  •   查询所有数据库 show dbs
  •   删除当前使用数据库 db.dropDatabase()
  •   从指定主机上克隆数据库 db.cloneDatabase("127.0.0.1")
  •   从指定的机器复制数据库数据到某个数据库 db.copyDatabase("mydb","temp","127.0.0.1")
  •   修复数据库 db.repairDatabase()
  •   查看当前使用的数据库 db.getNamedb
  •   显示当前数据库状态 db.stats()
  •   当前db版本 db.version()
  •   查看当前数据库机器地址 db.getMongo()  326电影网

  3.2 集合操作

  •   创建集合 db.createCollection('collName',{size:20,capped:5,max:100}) 创建成功会显示{"ok":1}
  •   得到指定名称的集合 db.getCollection("account")
  •   得到当前数据库所有集合 db.getCollectionNames()
  •   得到当前数据库所有集合索引的状态 db.printCollectionStats()

  3.3 用户相关

  •   添加用户 db.addUser("name")db.addUser("userName","pwd123",true) 设置密码、是否只读
  •   数据库认证 db.auth("userName","123123")
  •   显示当前所用用户 show users
  •   删除用户 db.removeUser("userName")

  3.4 集合查询

  •   查询所用记录 db.userInfo.find() 默认每页显示20条记录
  •   查询去掉后的当前集合中的某列的重复数据 db.userInfo.disinct("name")
  •   等于 db.userInfo.find({"gae":22}) 查询age==22的集合
  •   大于 db.userInfo.find({"age":{$gt:22}})
  •   小于 db.userInfo.find({"age":{$lt:22}})  
  •   包含 db.userInfo.find({name:/mongo/})性喜剧
  •   开头 db.userInfo.find({name:/^mongo/})
  •   查询指定列name、age db.userInfo.find({},{name:1,age:1})
  •   查询指定数据 db.userInfo.find({age:{$gt25}},{name:1,age:1})
  •   升序 db.userInfo.find().sort({age:1})
  •   降序 db.userInfo.find().sort({age:-1})
  •   查询前5条数据 db.userInfo.find().limit(5)
  •   查询10条以后的数据 db.userInfo.find().skip(10)
  •   查询5-10条数据 db.userInfo.find().limit(10).skip(5)
  •   与查询 db.userInfo.find({$or:[{age:22},{age:25}]})
  •   查询第一条数据 db.userInfo.findOne()
  •   查询集合的条数 db.userInfo.find().count()
  •   按某列进行排序 db..find({sex:{$exists:true}}).count()
  •     group  db.userInfo.group({"key": {"age": true},"initial": {"user": []},reduce": function(cur,prev){prev.user.push(cur.name);}
        参数选项:

             1.key: 这个就是分组的key 2.initial: 每组都分享一个初始化函数,特别注意:是每一组initial函数。 3.reduce: 这个函数的第一个参数是当前的文档对象,第二个参数是上一次function操作的累计对象。有多少个文档, $reduce就会调用多少次。            4.condition: 这个就是过滤条件。 5.finalize: 这是个函数,每一组文档执行完后,多会触发此方法。

  3.5 索引

  •   添加索引 db.users.ensureIndex({"name":1}) ensureIndex方法参数中,数字1表示升序,-1表示降序。326影视
  •   唯一索引 db.users.ensureIndex({"name":1},{"unique":true})
  •   组合索引 db.users.ensureIndex({"name":1, "age":-1})
  •   查看索引 db.users.getIndexes()
  •   查询索引 db.users.find({"name":"k"}).hint({"name":1,"age":1})
  •   删除索引 db.users.dropIndexes()
  •   删除指定索引 db.users.dropIndex("name_1")
作者: 【逸然微笑】
出处: http://www.cnblogs.com/weixiao520/
主要研究技术:专注于Winform开发、Web开发。
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值