A few months ago, we introduced MongoDB for VS Code, an extension to quickly connect to MongoDB and Atlas and work with your data right inside your code editor.
Since then, over 85,000 of you have installed the extension, and based on your feedback, we improved the extension quite a bit and released a few new versions that added new functionality and extended the existing one.
With this week’s release, we close the loop on what you can do with MongoDB for VS Code:
- You can choose the database and collection you want to query
- Make sure that you have the right indexes in place (an create new indexes if you don't)
- Search for documents with playgrounds
- Update your documents directly in the editor
All of this with a workflow that is well integrated with the native VS Code functionality and its shortcut.
When you work with your data in MongoDB, no matter if you are building a cool application or if you are writing a Playground for an analytics query, you want to make sure your queries are covered by the right indexes. In MongoDB for VS Code that’s extremely easy to do: just select the collection you want in the tree view and all the information you need is right there.
And if you see that the index you need is missing, we can prefill a playground with the command you need to create it.
Quick Access to Document Search
Once the right indexes are in place, jumping into a prefilled playground to find documents is just one click away.
From there, you can just customize your query to find the documents you need.
Results of a query – or of any playground, really – can also be saved into a file for later use or to share it with your colleagues. Just hit Ctrl/Cmd+S like you’d normally do with any file in VS Code and you are done.
After you’ve found the documents you were looking for, you can open each one in its own editor view, edit it and save it back into MongoDB.
Document editing was our most requested feature, and we are happy to have finally shipped it in our most recent release of MongoDB for VS Code and to have it built it in a way that fits within the natural VS Code user experience.
VS Code Playgrounds + Node.js API + NPM Modules
What I described above, is a normal flow when you work with a database: pick your db and collection, find the documents you are interested in, edit them and finally save the changes back to the database.
MongoDB playgrounds are much more powerful than that though. First of all, you can use them to run any command that you’d run in the MongoDB shell: this means that playgrounds are effectively a shell replacement and a great way to write, edit and save long shell scripts in a full-featured editor.
Second, in playgrounds, you have the entire Node.js API available to you and with a bit more work you can even require any module from NPM inside your playground code.
Here’s how it’s done.
Go to VS Code’s local settings folder ($HOME/.vscode on Linux/macOS or %USERPROFILE%/.vscode on Windows) and install a module from NPM. We’ll use cowsay as an example:
$ npm i cowsay
Now, go back to VS Code, connect to a MongoDB server or to an Atlas cluster and create the following playground:
const cowsay = require('cowsay');
text : db.version()
If everything works as expected, a cute cow should tell you what version of the server you are currently using.
As you can see, the result of a playground does not have to be JSON documents. It can really be anything, and you can use any node modules out there to format it in the way you need it. This coupled with other VS Code extensions (one that I like a lot is Charts.js Preview for example) can make VS Code a powerful tool to query and analyze your data stored in MongoDB.
Try it Now!
If you are a VS Code user, getting started with MongoDB for VS Code is easy:
- Install the extension from the marketplace
- Get a free Atlas cluster if you don’t have a MongoDB server already
- Connect to it and start doing cool stuff with playgrounds
You can find more information about MongoDB for VS Code and all its features in the documentation.