Getting Started with the MongoDB Extension for VS Code
Tags: vscode, mongodb, nosql
Published: 2020-05-19 20:18:19

Last week MongoDB [announced](https://www.mongodb.com/blog/post/introducing-mongodb-for-vs-code) 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](https://www.mongodb.com/blog/post/introducing-mongodb-for-vs-code)

To get started, install the extension via the [VS Code Marketplace] (https://marketplace.visualstudio.com/items?itemName=mongodb.mongodb-vscode). 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](https://dev-to-uploads.s3.amazonaws.com/i/fkx95b382h2vxd5a772e.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/au3jorcduif6gnmo0twh.png)

![Connect to MongoDB](https://dev-to-uploads.s3.amazonaws.com/i/bvy6m916x2skn4u5pdau.png)


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

![Connect to MongoDB Cluster](https://dev-to-uploads.s3.amazonaws.com/i/sinjed041v0mtcacn3xi.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/2m2r6vrelhq4ecvjzvcx.png)

![Copy Connection String](https://dev-to-uploads.s3.amazonaws.com/i/0y4l03b33h2zly6s3zil.png)

![Enter connection string](https://dev-to-uploads.s3.amazonaws.com/i/she7n3enrn9w6chqbrma.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/8s7eo2acjbbee839viqz.png)


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](https://dev-to-uploads.s3.amazonaws.com/i/ik8d4wghmplkcbcbcq05.png)


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](https://dev-to-uploads.s3.amazonaws.com/i/bb36bb0f832nwsxzb2s9.png)


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