To get the most out of MongoDB, you’re going to need to become familiar with the MongoDB Query Language (MQL). No, it is not like SQL if you’re familiar with relational database management systems (RDBMS), but it isn’t any more difficult. MQL can be used from the CLI, Visual Studio Code, the development drivers, and more. You’ll get the same experience no matter where you’re trying to write your queries.
In this section, we’re going to focus on Visual Studio Code and the MongoDB Playground extension for managing our data. We’re doing this because Visual Studio Code is common developer tooling and it makes for an easy to use experience.
While we could write our queries out of the box with Visual Studio Code, we won’t be able to interact with MongoDB in a meaningful way until we install the MongoDB Playground extension.
Within Visual Studio Code, bring up the extensions explorer and search for MongoDB (File > Preferences > Extensions).
Install the official extension with MongoDB as the publisher.
With the extension installed, we’ll need to interact with it from within Visual Studio Code. There are a few ways to do this, but we’re going to use the command palette.
Open the command pallette (CMD + SHIFT + P, if you’re on macOS), and enter MongoDB: Connect into the input box.
You’ll be able to enter the information for your particular MongoDB cluster. Once connected, we can proceed to creating a new Playground. If you’ve already saved your information into the Visual Studio Code extension and need to connect later, you can always enter Show MongoDB in the command pallette and connect.
Assuming we’re connected, enter Create MongoDB Playground in the command pallette to create a new file with boilerplate MQL.
See the directory,
mongodb-playgorunds/ for existing scripts.
Within Visual Studio Code, you can highlight the MQL inside a script and choose Run Selected Lines From Playground or use the command pallette to run the entire playground (i.e.
MongoDB: Run All From Playground). There is also a play button to the top-right of the editor when you have a playground
.mongodb file open.