MongoDB 创建数据库

MongoDB 教程 · 2019-03-05 20:14:42

语法

MongoDB 创建数据库的语法格式如下:

use DATABASE_NAME

如果数据库不存在,则创建数据库,否则切换到指定数据库。

实例

以下实例我们创建了数据库 codercto:

> use codercto
switched to db codercto
> db
codercto
> 

如果你想查看所有数据库,可以使用 show dbs 命令:

> show dbs
admin   0.000GB
local   0.000GB
> 

可以看到,我们刚创建的数据库 codercto 并不在数据库的列表中, 要显示它,我们需要向 codercto 数据库插入一些数据。

> db.codercto.insert({"name":"码农教程"})
WriteResult({ "nInserted" : 1 })
> show dbs
local   0.078GB
codercto  0.078GB
test    0.078GB
> 

MongoDB 中默认的数据库为 test,如果你没有创建新的数据库,集合将存放在 test 数据库中。

注意: 在 MongoDB 中,集合只有在内容插入后才会创建! 就是说,创建集合(数据表)后要再插入一个文档(记录),集合才会真正创建。

点击查看所有 MongoDB 教程 文章: https://www.codercto.com/courses/l/32.html

查看所有标签

Remote

Remote

Jason Fried、David Heinemeier Hansson / Crown Business / 2013-10-29 / CAD 26.95

The “work from home” phenomenon is thoroughly explored in this illuminating new book from bestselling 37signals founders Fried and Hansson, who point to the surging trend of employees working from hom......一起来看看 《Remote》 这本书的介绍吧!

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具