DEV Community

F.J.
F.J.

Posted on

Getting Started with the MongoDB Extension for VS Code

Last week MongoDB announced the release of their extension "MongoDB for VS Code". Naturally, I was very excited to read the news given my love for VS Code and all things MongoDB.

The new MongoDB extension allow us to:

Connect to a MongoDB or Atlas cluster, navigate through your databases and collections, get a quick overview of your schema, and see the documents in your collections

Create MongoDB Playgrounds, the fastest way to prototype CRUD operations and MongoDB commands

Quickly access the MongoDB Shell, to launch the MongoDB Shell from the command palette and quickly connect to the active cluster.

Source: MongoDB

To get started, install the extension via the VS Code Marketplace. We can also install it by opening Extensions tab in VS Code (CMD + Shift +X on Mac or Ctrl + Shift + X on Windows) and searching for "MongoDB".

MongoDB Extension for VS Code

Once installed there will be a new MongoDB tab that we can use to add our connections by clicking "Add Connection". If you've used MongoDB Compass before, then the form should be familiar. You can enter your connection details in the form, or use a connection string. I went with the latter as my database is hosted on MongoDB Atlas.

Add new MongoDB connection

Connect to MongoDB

To obtain your connection string, navigate to your "Clusters" page and select "Connect".

Connect to MongoDB Cluster

Choose the "Connect using MongoDB Compass" option and copy the connection string. Make sure to add your username and password in their respective places before entering the string in VS Code.

Connection Method

Copy Connection String

Enter connection string

Once you've connected successfully, you should see an alert. At this point, you can explore the data in your cluster, as well as your schemas.

Connected Successfully

We can now create a new playground by using the menu on the MongoDB tab and selecting the "Create MongoDB Playground option".

Create MongoDB Playground

The playground opens a template you can use or modify. Here I've written a few commands to add data to my test database and a simple find query. The playground is the perfect place to test your queries and aggregation pipelines. The results are shown in the Output window.

Playground Example

And that's it! Congratulations, you have now created your first successful connection to your MongoDB database using the new extension! Happy hacking!

Top comments (0)