Flying blind with a database is everyone’s least favorite part of development since the invention of computers. This is why we have database admin tools for MongoDB. However, jumping between your text editor and your database admin tool is a tad disruptive. Kind of like when your four year old walks in while you are being interviewed on the news.
But did you know that you can turn Visual Studio Code into a killer MongoDB admin tool? You can, and all you need is the Cosmos DB extension.
Cosmos DB Extension
The CosmosDB Extension was created by Microsoft to make it easier to manage and work with CosmosDB databases inside of Visual Studio Code. But since CosmosDB supports the MongoDB API, the extension works perfectly for any MongoDB database.
This article will treat CosmosDB as just another MongoDB database. You can get a free Azure CosmosDB Database here.
Note that you need to have MongoDB and the Mongo Shell installed locally for the extension to work. The Community Edition will work just fine and you can find it along with install instructions here.
Install the Cosmos DB extension from the extension gallery by searching for “cosmos”.
Once the extension is installed, you can see any of your CosmosDB instances in Azure, but you can also connect to any MongoDB instance-either local or remote.
You can browse the data in your collections just by selecting the collection in the sidebar.
If you’re using CosmosDB, you can create a new CosmosDB database in Azure directly from the extension.
Mongo Scrapbooks are a great way to test your queries and work with your data locally inside of VS Code. To create a new Scrapbook, open the Command Palette (⌘ + ⇧ + P) and type “mongo”. You will see an option for Mongo DB scrapbooks…
If VS Code asks you for your Mongo DB Shell path, you can place the following line your User Settings file (⌘,). Being on a Mac, my path was `/usr/local/bin/mongo`.
You need to make sure you are connected to a MongoDB instance in order to use your scrapbook. Right-click one of your MongoDB databases in the sidebar and select “Connect”. The scrapbooks provide intellisense to not only all of the available MongoDB methods, but also your collections.
To execute commands, open the Command Palette (⌘ + ⇧ + P) and type “execute” to find the “MongoDB: Execute Command” action. Alternatively, you can just use the ⌘ + ⇧ + ‘ keyboard shortcut. Results show up in a new pane to the right.
It’s actually possible to edit these results in place. Simply make a change to the document in the results pane, right-click and select “Update”. The changes are persisted to the database.
You can also use the extension to open a new Mongo Shell session (integrated terminal), connect to MongoDB databases by URL and many of the other actions that we’ve already discussed here.
The CosmosDB extension for Visual Studio Code turns your text editor into a killer MongoDB administration tool as well. And that’s great, because the best place to work with your database is in the same place where you write all your code.