Use Mongo Atlas and Compass to quickly go from data to code

Many people have heard of MongoDB, many people use MongoDB, but should you?

I’ll start with the caveat emptor, I have used mongodb for years now, I still have occasion to use SQL, but in the work that I have been involved with the document model has worked quite well. To be concise, if most of the fields of a document are always needed it is an easier approach to use a document database, at least in my opinion. What I find particularly useful with mongodb is that data can be out of the database in very creative ways and then plugged immediately into another program. In this example, I show how geodata can be summarized and then made into a graph with a little help from python and R. I have further plans for this project and that will be the subject of future blog posts. Please enjoy and any feedback or questions are welcome.

Now that is out of the way I can demonstrate some integrations of data into different applications, all centering around the primary tool a developer would use with mongodb: Compass. For purposes of this series of blog posts, I’ll be Compass to access data on Atlas, the MongoDB cloud service. Use the link to download Compass for your system, then go to atlas.mongodb.com

It is easy and free to start with Mongo on the cloud. In addition, I highly recommend also sign up to the Mongo University. I completed the Java Developer’s course and while much of the information was familiar and known, I still learned some important things and better appreciate the finer details of using and managing a cloud-managed mongo database. As part of the set-up of the Java course, you need to set up a new atlas cluster with a copy of several interesting data collections. For the java course, the work is based on the sample_mflix database, but for this first set of posts, I’ll be using the sample_restaurants database which has two collections: restaurants and neighborhoods.

Here is the first screen you need to deal with:

First Atlas screen

Go to Database Access and create a new user using the option of username/password. Unless you plan to share your password with others (don’t do that) there is no problem with read/write access to all collections. The next thing is to whitelist an IP so that there is access to the outside world. If you have a static IP, use the option for my IP:

Note the purple arrow on the right-side. The “Add IP Address” is the entry point.

Now you are ready to create a connection string for Compass that points to your Atlas deployment. That simple.

To get a connection string:

1. Open up the IP whitelist and add either your IP, a static IP, or 0.0.0.0

2. Go to the Database Access link and create a new username/password,
record the username and password created in a file. The password can only be replaced, not edited, or viewed

3. Go to the ‘Database’ link on the top -left and press the ‘Connect’ button and copy the link

On your local machine, open Compass and go to ‘new connection’ paste in the link. It helps to use the edit fields so that you can easily see both the username and password. Now you are ready to start using your cluster from your local machine remotely accessing the Atlas cloud.

Way-station summary:

You now have the ability to view and query the data. There are many features in Compass, you can add records from a csv or json file, edit and delete individual records, but the feature I want to use for integration is the Aggregation function. It is worth reading up on the major stages of Aggregation here.

I’ll go briefly over my aggregation code, but the importance is that a near-workable program is created from within Compass.

And here is the code (final):

See the Gist for the code

Most of the code above was directly copied from the export code window in Compass. With a small amount of helper code to make the data suitable to pass on to R. Running the code on my local machine yields the following:

Using the python function from R (Studio)

Tables are a bit boring, so how about some graphs of the data. R is a good sounding board for graphing results and refining what you want to present in your data. So, the question is how to get

the data into R and into graphs? The project reticulate is exactly the right tool. The code is in the Gist above.

This presents two graphs, one using the summary creating a bar graph:

The graph for the listings is a map:

The graph is partly interactive. The next goal would be to show the two graphs on the same panel and to have the option to input a new center coordinate and distance from the interface. An even further step would be to put this into either a web application or a mobile app. Stay tuned.

Video:

https://youtu.be/23hBA5dSPas

Mongo is me

Ilan Toren can be reached via my super-secret alias Richard Thorne or just shout “Hey Mongo” and I might just answer.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store