创建数据库
1.1 use
use DATABASE_NAME 用于创建数据库。如果数据库不存在那么它将会创建一个新的的数据库,否则返回已经存在的数据库。eg: >> use mydb
switched to db mydb
1.2 db
检查当前选择的数据库
eg: >> dbmydb
1.3 show dbs
检查数据库列表eg: >>show dbs
local 0.78125GB
test 0.23012GB创建的数据库mydb 列表中是不存在的。要显示的数据库,需要把它插入至少一个文件。
2 删除数据库
MongoDB db.dropDatabase() 命令是用来删除一个现有的数据库。
2.1 db.dropDatabase()
这将删除选定的数据库。如果还没有选择任何数据库,然后它会删除默认的 ’ test’ 数据库
eg: >use mydb
switched to db mydb
db.dropDatabase()
{ “dropped” : “mydb”, “ok” : 1 }
show dbs
local 0.78125GB
test 0.23012GB
3 创建数据库
3.1 创建
db.createCollection(name, options)
参数 类型 描述
Name String 要创建的集合名称
Options Document (可选)指定有关内存大小和索引选项
eg:
use test
switched to db test
db.createCollection(“mycollection”)
{ “ok” : 1 }
3.2查看
可以检查通过使用创建的集合命令 show collections
show collections
mycollection
system.indexesdb.createCollection(“mycol”, { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
{ “ok” : 1 }
在MongoDB中,不需要创建集合。当插入一些文件 MongoDB 自动创建的集合。
db.yiibai.insert({“name” : “yiibai”})
show collections
mycol
mycollection
system.indexes
yiibai
4 删除集合
MongoDB 的 db.collection.drop() 是用来从数据库中删除一个集合。
db.COLLECTION_NAME.drop()
eg:
db.mytest.drop()
true
show collections
system.indexes
wid
widgets
wigets
本文详细介绍了MongoDB中创建、删除数据库及集合的基本操作方法,包括如何使用use、db、showdbs命令进行数据库切换与检查,以及db.dropDatabase()与db.createCollection()等关键命令的使用场景。
234

被折叠的 条评论
为什么被折叠?



