Create Databases and Collections
You can create databases and collections using a MongoDB Playground.
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:
Create a Database and Collection
Once you connect to your deployment using MongoDB for VS code, use the left navigation to:
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.
Fill in
const database
andconst collection
with names of your database and 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.
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
See also:
To add documents to the collection, see Create Documents with VS Code.
To learn more about creating time series collections using MongoDB playgrounds, see Create Time Series Collections with VS Code
Example
This example creates a database named grades
and a regular collection
named testscores
.
To use this example, start with a template from your MongoDB Playgrounds:
const database = 'grades'; const collection = 'testscores'; use(database); db.createCollection(collection);
In the example:
const database
declares the namegrades
for the database.const collection
declares the nametestscores
for the collection.use(database)
creates the newgrades
database.db.createCollection(collection)
creates the collectiontestscores
inside of thegrades
database.
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.
{ "ok": 1 }
The grades
database and testscores
collection also appear in your
left navigation: