【MongoDB】基本命令-数据库基本管理
- MongoDB帮助命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<pre class="prettyprint">[mdb@xxf ~]$ mongo MongoDB shell version: 3.2.1 connecting to: test > help db.help() help on db methods db.mycoll.help() help on collection methods sh.help() sharding helpers rs.help() replica set helpers help admin administrative help help connect connecting to a db help help keys key shortcuts help misc misc things to know help mr mapreduce show dbs show database names show collections show collections in current database show users show users in current database show profile show most recent system.profile entries with time >= 1ms show logs show the accessible logger names show log [name] prints out the last segment of log in memory, 'global' is default use <db_name> set current database db.foo.find() list objects in collection foo db.foo.find( { a : 1 } ) list objects in foo where a == 1 it result of the last line evaluated; use to further iterate DBQuery.shellBatchSize = x set default number of items to display on shell exit quit the mongo shell > |
- 显示当前数据库服务器上的数据库
1 2 3 |
<pre class="prettyprint">> show dbs local 0.000GB > |
- 切换、创建数据库
使用use命令即可切换数据库,当切换的数据库不存在时,那么即创建了此数据库,需要注意的是,此数据库创建只是在内存中创建,如果没有在数据库中创建任何集合,那么当退出时,此数据库也就随之删除了。
1 2 3 4 5 |
<pre class="prettyprint">> use qq switched to db qq > show dbs local 0.000GB > |
此时大家会发现,已经切换到qq数据库,但是,我们show dbs时,并未看到此数据库,因为数据库中没有创建任何对象,所以,此时数据库并未真正的创建。我们创建一张测试表后再看看效果。
1 2 3 4 5 6 7 |
<pre class="prettyprint">> db.t.insert({'name':'zhangsan','age':18}) WriteResult({ "nInserted" : 1 }) > > show dbs local 0.000GB qq 0.000GB > |
我们可以看到,此时已经存在qq数据库了。
- 显示数据库中所有集合
很显然,数据库中只有一个集合t(也就是关系型数据库中所谓的表)
1 2 3 |
<pre class="prettyprint">> show collections t > |
- 查询数据库统计信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<pre class="prettyprint">> use qq switched to db qq > > db.stats() { "db" : "qq", "collections" : 1, "objects" : 1, "avgObjSize" : 54, "dataSize" : 54, "storageSize" : 16384, "numExtents" : 0, "indexes" : 1, "indexSize" : 16384, "ok" : 1 } > |
- 查看当前使用的数据库
1 2 3 4 5 6 7 |
<pre class="prettyprint">> db.getName() qq > 或者输入db进行查看,效果是一样的。 > db qq > |
- 查看当前使用的MongoDB版本
1 2 3 |
<pre class="prettyprint">> db.version() 3.2.1 > |
- 删除数据库
1 2 3 4 5 6 |
<pre class="prettyprint">> db.dropDatabase(); { "dropped" : "qq", "ok" : 1 } > > show dbs local 0.000GB > |