Docs Menu
Docs Home
/
MongoDB for VS Code
/

Create Databases and Collections

On this page

  • Prerequisites
  • Create a Database and Collection
  • Example

You can create databases and collections using a MongoDB Playground.

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 connection to a MongoDB deployment.

  • Activate the connection to the MongoDB deployment.

Once you connect to your deployment using MongoDB for VS code, use the left navigation to:

  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.

  3. Fill in const database and const collection with names of your database and 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.

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:

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 name grades for the database.

  • const collection declares the name testscores for the collection.

  • use(database) creates the new grades database.

  • db.createCollection(collection) creates the collection testscores inside of the grades 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:

Image showing database and collection
click to enlarge

Back

Explore with Playgrounds