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连接到部署后,使用左侧导航:
Select an active connection and click the icon that appears.选择一个活动连接,然后单击出现的图标。A MongoDB playground automatically opens with a template form to create a database and a regular collection or a time series collection.MongoDB游乐场会自动打开一个模板表单,以创建数据库和常规集合或时间序列集合。Fill in用数据库和集合的名称填写const databaseandconst collectionwith names of your database and collection.const database和const collection。Uncomment the form of the collection you wish to create.取消对要创建的集合形式的注释。Fill in the collection fields according to your collection's specifications.根据集合规格填写集合字段。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
To add documents to the collection, see Create Documents with VS Code.要将文档添加到集合中,请参阅使用VS Code创建文档。To learn more about creating time series collections using MongoDB playgrounds, see Create Time Series Collections with VS Code要了解有关使用MongoDB游乐场创建时间序列集合的更多信息,请参阅使用VS Code创建时间序列集
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 databasedeclares the name声明数据库的名称gradesfor the database.grades。const collectiondeclares the name声明集合的名称testscoresfor the collection.testscores。use(database)creates the new创建新的gradesdatabase.grades数据库。db.createCollection(collection)creates the collection在testscoresinside of thegradesdatabase.grades数据库中创建集合testscores。
When you press the Play Button, MongoDB for VS Code shows the following results in the 当您按下“播放”按钮时,MongoDB for VS Code在Playground Results.json pane to confirm the creation of the database and collection.Playground Results.json窗格中显示以下结果,以确认数据库和集合的创建。
{
"ok": 1
}
The grades database and testscores collection also appear in your left navigation:grades数据库和testscores集合也出现在左侧导航中:
