MongoDB命令行操作
MongoDB命令行操作本文专门介绍MongoDB的命令行操作。其实,这些操作在MongoDB官网提供的Quick Reference上都有,但是英文的,为了方便,这里将其稍微整理下,方便查阅。这里用来做测试的是远端(10.77.20.xx)的Mongo数据库。1、登录和退出mongo命令直接加MongoDB服务器的IP地址(比如:mongo 10.77.20.xx),就可以利用Mongo的默认端口号(27017)登陆Mongo,然后便能够进行简单的命令行操作。至于退出,直接exit,然后回车就好了。[plain]$ mongo 10.77.20.xxMongoDB shell version: 2.0.4connecting to: 10.77.20.xx/test> show collections> exitbye从以上可以看出,登录后mongo会自动连上一个名为test的数据库。如果这个数据库不存在,那么mongo会自动建立一个名为test的数据库。上面的例子,由于Mongo服务器上没有名为test的db,因此,mongo新建了一个空的名为test的db。其中,没有任何collection。2、database级操作[plain]2.1 查看服务器上的数据库> show dbsadmin (empty)back_up (empty)blogtest 0.203125GBlocal 44.056640625GBtest (empty)2.2 切换数据库切换到blogtest数据库(从默认的test数据库)> use blogtestswitched to db blogtestmongo中,db代表当前使用的数据库。这样,db就从原来的test,变为现在的blogtest数据库。2.3 查看当前数据库中的所有集合> show collectionsbooksystem.indexesuser2.4 创建数据库mongo中创建数据库采用的也是use命令,如果use后面跟的数据库名不存在,那么mongo将会新建该数据库。不过,实际上只执行use命令后,mongo是不会新建该数据库的,直到你像该数据库中插入了数据。> use test2switched to db test2> show dbsadmin (empty)back_up (empty)blogtest 0.203125GBlocal 44.056640625GBtest (empty)到这里并没有看到刚才新建的test2数据库。> db.hello.insert({"name":"testdb"})该操作会在test2数据库中新建一个hello集合,并在其中插入一条记录。> show dbsadmin (empty)back_up (empty)blogtest 0.203125GBlocal 44.056640625GBtest (empty)test2 0.203125GB> show collectionshellosystem.indexes这样,便可以看到mongo的确创建了test2数据库,其中有一个hello集合。2.5 删除数据库> db.dropDatabase(){ "dropped" : "test2", "ok" : 1 }> show dbsadmin (empty)back_up (empty)blogtest 0.203125GBlocal 44.056640625GBtest (empty)2.6 查看当前数据库> dbtest2可以看出删除test2数据库之后,当前的db还是指向它,只有当切换数据库之后,test2才会彻底消失。3、collection级操作[plain]3.1 新建collection> db.createCollection("Hello"){ "ok" : 1 }> show collectionsHellosystem.indexes从上面2.4也可以看出,直接向一个不存在的collection中插入数据也能创建一个collection。> db.hello2.insert({"name":"lfqy"})> show collectionsHellohello2system.indexes3.2 删除collection> db.Hello.drop()true返回true说明删除成功,false说明没有删除成功。> db.hello.drop()false不存在名为hello的collection,因此,删除失败。3.3 重命名collection将hello2集合重命名为HELLO> show collectionshello2system.indexes> db.hello2.renameCollection("HELLO"){ "ok" : 1 }> show collectionsHELLOsystem.indexes3.4 查看当前数据库中的所有collection>show collections3.5 建立索引在HELLO集合上,建立对ID字段的索引,1代表升序。>db.HELLO.ensureIndex({ID:1})4、Record级的操作这一小节从这里开始,我们用事先存在的blogtest数据库做测试,其中有两个Collection,一个是book,另一个是user。4.1 插入操作[plain]4.1.1 向user集合中插入两条记录> db.user.insert({'name':'Gal Gadot','gender':'female','age':28,'salary':11000})> db.user.insert({'name':'Mikie Hara','gender':'female','age':26,'salary':7000})4.1.2 同样也可以用save完成类似的插入操作> db.user.save({'name':'Wentworth Earl Miller','gender':'male','age':41,'salary':33000})4.2 查找操作4.2.1 查找集合中的所有记录[plain]> db.user.find(){ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13 }4.2.2 查找集合中的符合条件的记录[plain](1)单一条件a)Exact Equal:查询age为了23的数据> db.user.find({"age":23}){ "_id" : ObjectId("52442736d8947fb501000001"), "name" : "lfqy", "gender" : "male", "age" : 23, "salary" : 15 }b)Great Than:查询salary大于5000的数据> db.user.find({salary:{$gt:上一个:hadoop单机安装
下一个:hsql使用架构包启动数据库
- 更多DB2疑问解答:
- sql语句 从DB2移动到其他数据库
- DB2 character类型更新报错
- db2 怎么查看执行的sql语句需要多少系统临时表空间?
- db2数据库的table表里,一money字段中的数据显示为:56000,00 本人想要用sql语句将它修改为56,000,000
- DB2导出表结构,能不能只导出指定的表结构。求大神指教,给出命令哈
- DB2数据库,增加数据表空间容量需要重启数据库吗?
- cognos连接db2,为什么启动不了服务啊, 连接Oracle可以启动服务。。
- PostgreSQL对比DB2
- db2如何查看编写好的存储过程?
- db2数值截取
- 谁知道关于DB2怎么看系统日志,归档日志的位置。并且怎么修改日志呢?
- db2怎样重组表结构?
- 求一本【牛新庄的DB2运维教程】?
- db2 的 SECADM权限 是干什么的
- DB2怎么删除表空间所以数据