命令:use 数据库名 如果库不存在则创建,如果库已经存在则切换进入
> use admin switched to db admin >登录后,使用show dbs查看所有库
> db.auth('pandafox','123456') 1 > show dbs admin 0.000GB config 0.000GB local 0.000GB test 0.000GB切换到对应库,执行db.dropDatabase()命令
> use test switched to db test > db.dropDatabase() { "dropped" : "test", "ok" : 1 }MongoDB 中的集合是一组文档的集,相当于关系型数据库中的表。语法格式db.createCollection(name, options), name表示集合名称,options:是可选参数, 指定有关内存大小及索引的选项。 options选项:
字段类型描述capped布尔(可选)如果为 true,则创建固定集合。固定集合是指 有着固定大小的集合,当达到最大值时,它会自动覆盖最早 的文档。当该值为 true 时,必须指定 size 参数。autoindexid布尔(可选)如为 true,自动在 _id 字段创建索引。默认为 false。size数值(可选)为固定集合指定一个最大值(以字节计)。 如果 capped 为 true,也需要指定该字段。max数值(可选)指定固定集合中包含文档的最大数量。直接插入数据时,可创建集合,如下面创建的col01集合
> db.col01.insert({'name':'zhangsan'}) WriteResult({ "nInserted" : 1 }) > show collections col01使用db.createCollection('集合名称')创建不带参数集合
> db.createCollection('col02') { "ok" : 1 } > show collections col01 col02创建col03固定集合,集合空间大小为 1000000kb, 文档最大个数为2000
> db.createCollection('col03',{capped:true,autoIndexId:true,size:1000000,max:2000}) { "note" : "the autoIndexId option is deprecated and will be removed in a future release", "ok" : 1 } > show collections col01 col02 col03show collections与 show tables都可以
使用db.集合名.stats()
> db.col03.stats() { "ns" : "test.col03", "size" : 0, "count" : 0, "storageSize" : 4096, "capped" : true, "max" : 2000, "maxSize" : 1000192, "sleepCount" : 0, "sleepMS" : 0, "wiredTiger" : { "metadata" : { 省略...使用db.集合名.drop(),需要先切换到对应库
> db.col01.drop() true