ITPub博客

首页 > 数据库 > NoSQL > mongodb系列-数据库的创建和删除

mongodb系列-数据库的创建和删除

原创 NoSQL 作者:a3zi 时间:2015-02-18 19:01:25 0 删除 编辑

mongodb创建数据库可以直接用use

先看看有哪些db:

> show dbs

admin  (empty)

local  0.078GB

mydb   0.078GB

test   (empty)

> 

再新建一个数据库mytestdb:

> use mytestdb

switched to db mytestdb

> 

 

如果什么都不操作离开的话,这个库就会被系统删除:

> show dbs

admin  (empty)

local  0.078GB

mydb   0.078GB

test   (empty)

> 

如果插入一条语句,就可以保存下来:

在mydb库中的user表中插入一条记录

> db.user.insert({name:'jack'})

WriteResult({ "nInserted" : 1 })

查看有哪些db库

> show dbs

admin     (empty)

local     0.078GB

mydb      0.078GB

mytestdb  0.078GB

test      (empty)

切换到mytestdb库

> use mytestdb

switched to db mytestdb

查看mytestdb库中的所有表

> show collections

system.indexes

user

查看user表内容:

> db.user.find()

{ "_id" : ObjectId("54deb3fcf0d67ffc85508f9a"), "name" : "jack" }

> 

 

表的删除:

删除user表

> db.user.drop();

true

检查mytest库中的表,user已经不存在了

> show collections

system.indexes

> 

删除mytestdb库

> use mytestdb

switched to db mytestdb

> db.dropDatabase()

{ "dropped" : "mytestdb", "ok" : 1 }

> show dbs

admin  (empty)

local  0.078GB

mydb   0.078GB

test   (empty)

数据库mytestdb已经被删除

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29806344/viewspace-1438399/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2014-08-15

  • 博文量
    45
  • 访问量
    228904