Designing, Developing, and Analyzing with the New MongoDB Shell
Rate this article
There are many methods available for interacting with MongoDB and depending on what you're trying to accomplish, one way to work with MongoDB might be better than another. For example, if you're a power user of Visual Studio Code, then the MongoDB Extension for Visual Studio Code might make sense. If you're constantly working with infrastructure and deployments, maybe the MongoDB CLI makes the most sense. If you're working with data but prefer a command line experience, the MongoDB Shell is something you'll be interested in.
The MongoDB Shell gives you a rich experience to work with your data through syntax highlighting, intelligent autocomplete, clear error messages, and the ability to extend and customize it however you'd like.
In this article, we're going to look a little deeper at the things we can do with the MongoDB Shell.
If you're like me, looking at a wall of code or text that is a single color can be mind-numbing to you. It makes it difficult to spot things and creates overall strain, which could damage productivity. Most development IDEs don't have this problem because they have proper syntax highlighting, but it's common for command line tools to not have this luxury. However, this is no longer true when it comes to the MongoDB Shell because it is a command line tool that has syntax highlighting.
When you write commands and view results, you'll see colors that match your command line setup as well as pretty-print formatting that is readable and easy to process.
Formatting and colors are only part of the battle that typical command line advocates encounter. The other common pain-point, that the MongoDB Shell fixes, is around autocomplete. Most IDEs have autocomplete functionality to save you from having to memorize every little thing, but it is less common in command line tools.
As you're using the MongoDB Shell, simply pressing the "Tab" key on your keyboard will bring up valuable suggestions related to what you're trying to accomplish.
Syntax highlighting, formatting, and autocomplete are just a few small things that can go a long way towards making the developer experience significantly more pleasant.
How many times have you used a CLI, gotten some errors you didn't understand, and then either wasted half your day finding a missing comma or rage quit? It's happened to me too many times because of poor error reporting in whatever tool I was using.
With the MongoDB Shell, you'll get significantly better error reporting than a typical command line tool.
In the above example, I've forgotten a comma, something I do regularly along with colons and semi-colons, and it told me, along with providing a general area on where the comma should go. That's a lot better than something like "Generic Runtime Error 0x234223."
If you use the MongoDB Shell enough, you'll probably reach a point in time where you wish it did something specific to your needs on a repetitive basis. Should this happen, you can always extend the tool with snippets, which are similar to plugins.
To get an idea of some of the official MongoDB Shell snippets, execute the following from the MongoDB Shell:
1 snippet search
You can always define your own repository of snippets, but if you wanted to use one of the available but optional snippets, you could run something like this:
1 snippet install analyze-schema
The above snippet allows you to analyze any collection that you specify. So in the example of my "recipes" collection, I could do the following:
1 use food; 2 schema(db.recipes);
The results of the schema analysis, at least for my collection, is the following:
1 ┌─────────┬───────────────┬───────────┬────────────┐ 2 │ (index) │ 0 │ 1 │ 2 │ 3 ├─────────┼───────────────┼───────────┼────────────┤ 4 │ 0 │ '_id ' │ '100.0 %' │ 'ObjectID' │ 5 │ 1 │ 'ingredients' │ '100.0 %' │ 'Array' │ 6 │ 2 │ 'name ' │ '100.0 %' │ 'String' │ 7 └─────────┴───────────────┴───────────┴────────────┘
Snippets aren't the only way to extend functionality within the MongoDB Shell. You can also use Node.js in all its glory directly within the MongoDB Shell using custom scripts.
So let's say you've got a data need that you can't easily accomplish with the MongoDB Query API or an aggregation pipeline. If you can accomplish what you need using Node.js, you can accomplish what you need in the MongoDB Shell.
Let's take this example.
Say you need to consume some data from a remote service and store it in MongoDB. Typically, you'd probably write an application, download the data, maybe store it in a file, and load it into MongoDB or load it with one of the programming drivers. You can skip a few steps and make your life a little easier.
Try this.
When you are connected to the MongoDB Shell, execute the following commands:
1 use pokemon 2 .editor
The first will switch to a database—in this case, "pokemon"—and the second will open the editor. From the editor, paste in the following code:
1 async function getData(url) { 2 const fetch = require("node-fetch"); 3 const results = await fetch(url) 4 .then(response => response.json()); 5 db.creatures.insertOne(results); 6 }
The above function will make use of the node-fetch package from NPM. Then, using the package, we can make a request to a provided URL and store the results in a "creatures" collection.
You can execute this function simply by doing something like the following:
1 getData("https://pokeapi.co/api/v2/pokemon/pikachu");
If it ran successfully, your collection should have new data in it.
In regards to the NPM packages, you can either install them globally or to your current working directory. The MongoDB Shell will pick them up when you need them.
If you'd like to use your own preferred editor rather than the one that the MongoDB Shell provides you, execute the following command prior to attempting to open an editor:
1 config.set("editor", "vi");
The above command will make VI the default editor to use from within the MongoDB Shell. More information on using an external editor can be found in the documentation.
You can do some neat things with the MongoDB Shell, and while it isn't for everyone, if you're a power user of the command line, it will certainly improve your productivity with MongoDB.