Docs Home / VS Code Extension / Explore with Playgrounds

Create Databases and Collections创建数据库和集合

You can create databases and collections using a MongoDB Playground.您可以使用MongoDB游乐场创建数据库和集合

Prerequisites先决条件

If you have not done so already, you must complete the following prerequisites before you can create a database or collection with a MongoDB Playground:如果您还没有这样做,则必须完成以下先决条件,然后才能使用MongoDB Playground创建数据库或集合:

Create a Database and Collection创建数据库和集合

Once you connect to your deployment using MongoDB for VS code, use the left navigation to:使用MongoDB for VS Code连接到部署后,使用左侧导航:

  1. Select an active connection and click the icon that appears.选择一个活动连接,然后单击出现的图标。
  2. A MongoDB playground automatically opens with a template form to create a database and a regular collection or a time series collection.MongoDB游乐场会自动打开一个模板表单,以创建数据库和常规集合或时间序列集合
  3. Fill in const database and const collection with names of your database and collection.用数据库和集合的名称填写const databaseconst collection
  4. Uncomment the form of the collection you wish to create.取消对要创建的集合形式的注释。
  5. Fill in the collection fields according to your collection's specifications.根据集合规格填写集合字段。
  6. To run the playground, click the Play Button at the top right of the VS code navigation bar.要运行游乐场,请单击VS Code导航栏右上角的“播放”按钮。

After running the playground, the left navigation updates with new database listed under the active connection it was created. You can find the newly created collection by expanding the new database.运行游乐场后,左侧导航将使用创建的活动连接下列出的新数据库进行更新。您可以通过展开新数据库来找到新创建的集合。

Tip

Example示例

This example creates a database named grades and a regular collection named testscores.此示例创建了一个名为grades的数据库和一个名为了testscores的常规集合。

To use this example, start with a template from your MongoDB Playgrounds:要使用此示例,请从MongoDB Playgrounds中的模板开始:

const database = 'grades';
const collection = 'testscores';

use(database);

db.createCollection(collection);

In the example:在示例中:

  • const database declares the name grades for the database.声明数据库的名称grades
  • const collection declares the name testscores for the collection.声明集合的名称testscores
  • use(database) creates the new grades database.创建新的grades数据库。
  • db.createCollection(collection) creates the collection testscores inside of the grades database.grades数据库中创建集合testscores

When you press the Play Button, MongoDB for VS Code shows the following results in the Playground Results.json pane to confirm the creation of the database and collection.当您按下“播放”按钮时,MongoDB for VS Code在Playground Results.json窗格中显示以下结果,以确认数据库和集合的创建。

{
"ok": 1
}

The grades database and testscores collection also appear in your left navigation:grades数据库和testscores集合也出现在左侧导航中:

Image showing database and collection