MongoDB’s flexible schema and rich document structure allow developers to quickly build applications with rich data structures. However, this flexibility can also make it difficult to understand the structure of the data in an existing database.
Until now, if you wanted to understand the structure of your data, you would have to use the MongoDB shell to issue queries and view data at the command line. There has to be a better way -- enter MongoDB Compass.
What is MongoDB Compass?
MongoDB 3.2 introduces MongoDB Compass -- a graphical tool that allows you to easily analyse and understand your database schema, as well as allowing you to visually construct queries, all without having to know MongoDB’s query syntax:
MongoDB Compass was built to address 3 main goals:
- Schema discovery
- Data discovery
- Visual construction of queries
Compass displays the data types of fields in a collection’s schema. The example below is taken from a mock dataset that I use when test driving Compass. It reports that there are documents in the collection that contain a field last_login with the type date:
Compass also displays a percentage breakdown for fields with varying data types across documents. In this example, 81% of documents store phone_no as a string, and the remaining 19% store it as a number:
For sparse fields, where some documents omit a value, Compass displays the percentage of missing values as “undefined.” Here, the age field is missing in 40% of the sampled documents.
This is exceptionally useful to understand whether your application is storing data the way that you expect it to. Imagine the case where you have a field showing a mix of strings and numbers - perhaps there is an application bug somewhere that has crept in and is storing data with a different type than it should be?
Compass is able to show histograms to represent the data frequency and distribution within a collection. For example, here is a data set containing the age of users. We can see the minimum age is 16, the maximum age is 56 and the most popular age is late 30’s (the exact value is shown by hovering over the bar itself).
Here’s another example using a field that stores names. Compass will display a random selection of string values for the field:
Visual Construction of Queries
Do you want an easier way to type out a MongoDB query? Charts in Compass are fully interactive. Clicking on a chart value or bar will automatically build a MongoDB query that matches the selected range in the interface.
In the example below, clicking on the “JFK” bar builds a query matching all documents whose
departureAirportFsCode field matches “JFK”:
Clicking on other field values adds the field and range to the selection, creating a more complex query. Continuing with our example, we can select a particular
flightId in addition to departures from JFK Airport.
Once you hit the Apply button, Compass will execute the query and bring back the results! It’s as easy as it sounds. You can be building queries with a few clicks of a button in no time at all.
One final thing to mention - we didn’t forget about the JSON. Documents can be examined in the document viewing pane. This can be expanded by clicking on the Document Viewer icon on the right-hand side of the page:
I know you must be wondering - where can I get this thing?!
Well, MongoDB Compass is available in the download center on mongodb.com. It comes included for production use with our subscriptions, both MongoDB Professional and MongoDB Enterprise Advanced. MongoDB Compass can also be used for free in a development environment.
This is only version 1.0 of Compass - there is lots of great functionality to come. I’m super excited to be part of the Compass team and I can’t wait for the next set of releases.
Give MongoDB Compass a try today.
About the author - Sam Weaver
Sam Weaver is the Product Manager for Developer Experience at MongoDB based in New York. Prior to MongoDB, he worked at Red Hat doing technical presales on Linux, Virtualisation, and Middleware. Originally from Cheltenham, England, he received his Bachelors in Computer Science from Cardiff University. Sam has also cycled from London to Paris, competed in several extreme sports tournaments such as ToughMudder, and swam with great white sharks.