Docs 菜单
Docs 主页
/ /

创建数据库和集合

可以使用 MongoDB Playground 创建数据库和集合

如果尚未执行此操作,则必须先完成以下先决条件,然后才能使用 MongoDB Playground 创建数据库或集合:

  • 创建与 MongoDB 部署的连接。

  • 激活与 MongoDB 部署的连接。

使用 MongoDB for VS Code 连接到部署后,请使用左侧导航:

  1. 选择一个活动连接,然后单击出现的 图标。

  2. MongoDB playground 会自动打开模板表单,用于创建数据库和常规集合或时间序列集合

  3. const databaseconst collection 中填入您的数据库和集合名称。

  4. 取消注释您希望创建的集合形式。

  5. 根据集合规范填写集合字段。

  6. 要运行 Playground,请单击 VS Code 导航栏右上角的 Play Button

运行 playground 后,左侧导航更新,所创建的活动连接下会列出新的数据库。您可以通过展开新数据库来查找新创建的集合。

提示

此示例创建一个名为grades的数据库和一个名为testscores的常规集合。

要使用此示例,请从 MongoDB Playgrounds 中的模板开始:

const database = 'grades';
const collection = 'testscores';
use(database);
db.createCollection(collection);

在示例中:

  • const database 声明数据库的名称 grades

  • const collection 声明集合的名称 testscores

  • use(database) 创建新的 grades 数据库。

  • db.createCollection(collection)grades 数据库内创建集合 testscores

当您按 Play Button 时,MongoDB for VS Code 将在 Playground Results.json 窗格显示以下结果,确认数据库和集合的创建。

{
"ok": 1
}

grades 数据库和 testscores 集合也会显示在左侧导航栏中:

显示数据库和集合的图像
点击放大

后退

使用 Playgrounds 进行探索

在此页面上